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/assert-pre-internal.h>
33 #include <babeltrace/ctf-ir/clock-class-internal.h>
34 #include <babeltrace/ctf-ir/event-class-internal.h>
35 #include <babeltrace/ctf-ir/field-types-internal.h>
36 #include <babeltrace/ctf-ir/fields-internal.h>
37 #include <babeltrace/ctf-ir/stream-class-internal.h>
38 #include <babeltrace/ctf-ir/validation-internal.h>
39 #include <babeltrace/ctf-ir/visitor-internal.h>
40 #include <babeltrace/ctf-ir/utils.h>
41 #include <babeltrace/ctf-ir/utils-internal.h>
42 #include <babeltrace/ref.h>
43 #include <babeltrace/compiler-internal.h>
44 #include <babeltrace/align-internal.h>
45 #include <babeltrace/endian-internal.h>
46 #include <babeltrace/assert-internal.h>
52 int bt_stream_class_common_initialize(struct bt_stream_class_common
*stream_class
,
53 const char *name
, bt_object_release_func release_func
)
55 BT_LOGD("Initializing common stream class object: name=\"%s\"", name
);
57 bt_object_init(stream_class
, release_func
);
58 stream_class
->name
= g_string_new(name
);
59 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
60 (GDestroyNotify
) bt_object_release
);
61 if (!stream_class
->event_classes
) {
62 BT_LOGE_STR("Failed to allocate a GPtrArray.");
66 stream_class
->event_classes_ht
= g_hash_table_new_full(g_int64_hash
,
67 g_int64_equal
, g_free
, NULL
);
68 if (!stream_class
->event_classes_ht
) {
69 BT_LOGE_STR("Failed to allocate a GHashTable.");
73 BT_LOGD("Initialized common stream class object: addr=%p, name=\"%s\"",
82 void bt_stream_class_common_finalize(struct bt_stream_class_common
*stream_class
)
84 BT_LOGD("Finalizing common stream class: addr=%p, name=\"%s\", id=%" PRId64
,
85 stream_class
, bt_stream_class_common_get_name(stream_class
),
86 bt_stream_class_common_get_id(stream_class
));
87 bt_put(stream_class
->clock_class
);
89 if (stream_class
->event_classes_ht
) {
90 g_hash_table_destroy(stream_class
->event_classes_ht
);
92 if (stream_class
->event_classes
) {
93 BT_LOGD_STR("Destroying event classes.");
94 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
97 if (stream_class
->name
) {
98 g_string_free(stream_class
->name
, TRUE
);
101 BT_LOGD_STR("Putting event header field type.");
102 bt_put(stream_class
->event_header_field_type
);
103 BT_LOGD_STR("Putting packet context field type.");
104 bt_put(stream_class
->packet_context_field_type
);
105 BT_LOGD_STR("Putting event context field type.");
106 bt_put(stream_class
->event_context_field_type
);
110 void bt_stream_class_destroy(struct bt_object
*obj
)
112 struct bt_stream_class
*stream_class
;
114 stream_class
= (void *) obj
;
115 BT_LOGD("Destroying stream class: addr=%p, name=\"%s\", id=%" PRId64
,
116 stream_class
, bt_stream_class_get_name(stream_class
),
117 bt_stream_class_get_id(stream_class
));
118 bt_stream_class_common_finalize(BT_TO_COMMON(stream_class
));
119 g_free(stream_class
);
122 struct bt_stream_class
*bt_stream_class_create(const char *name
)
124 struct bt_stream_class
*stream_class
= NULL
;
127 BT_LOGD("Creating stream class object: name=\"%s\"", name
);
128 stream_class
= g_new0(struct bt_stream_class
, 1);
130 BT_LOGE_STR("Failed to allocate one stream class.");
134 ret
= bt_stream_class_common_initialize(BT_TO_COMMON(stream_class
),
135 name
, bt_stream_class_destroy
);
137 /* bt_stream_class_common_initialize() logs errors */
141 BT_LOGD("Created stream class object: addr=%p, name=\"%s\"",
146 bt_put(stream_class
);
150 struct bt_trace
*bt_stream_class_get_trace(struct bt_stream_class
*stream_class
)
152 return BT_FROM_COMMON(bt_stream_class_common_get_trace(
153 BT_TO_COMMON(stream_class
)));
156 const char *bt_stream_class_get_name(struct bt_stream_class
*stream_class
)
158 return bt_stream_class_common_get_name(BT_TO_COMMON(stream_class
));
161 int bt_stream_class_set_name(struct bt_stream_class
*stream_class
,
164 return bt_stream_class_common_set_name(BT_TO_COMMON(stream_class
),
168 int64_t bt_stream_class_get_id(struct bt_stream_class
*stream_class
)
170 return bt_stream_class_common_get_id(BT_TO_COMMON(stream_class
));
173 int bt_stream_class_set_id(struct bt_stream_class
*stream_class
, uint64_t id
)
175 return bt_stream_class_common_set_id(BT_TO_COMMON(stream_class
), id
);
179 void event_class_exists(gpointer element
, gpointer query
)
181 struct bt_event_class_common
*event_class_a
= element
;
182 struct search_query
*search_query
= query
;
183 struct bt_event_class_common
*event_class_b
= search_query
->value
;
186 if (search_query
->value
== element
) {
187 search_query
->found
= 1;
192 * Two event classes cannot share the same ID in a given
195 id_a
= bt_event_class_common_get_id(event_class_a
);
196 id_b
= bt_event_class_common_get_id(event_class_b
);
198 if (id_a
< 0 || id_b
< 0) {
199 /* at least one ID is not set: will be automatically set later */
204 BT_LOGW("Event class with this ID already exists in the stream class: "
205 "id=%" PRId64
", name=\"%s\"",
206 id_a
, bt_event_class_common_get_name(event_class_a
));
207 search_query
->found
= 1;
216 int bt_stream_class_common_add_event_class(
217 struct bt_stream_class_common
*stream_class
,
218 struct bt_event_class_common
*event_class
,
219 bt_validation_flag_copy_field_type_func copy_field_type_func
)
222 int64_t *event_id
= NULL
;
223 struct bt_trace_common
*trace
= NULL
;
224 struct bt_stream_class_common
*old_stream_class
= NULL
;
225 struct bt_validation_output validation_output
= { 0 };
226 struct bt_field_type_common
*packet_header_type
= NULL
;
227 struct bt_field_type_common
*packet_context_type
= NULL
;
228 struct bt_field_type_common
*event_header_type
= NULL
;
229 struct bt_field_type_common
*stream_event_ctx_type
= NULL
;
230 struct bt_field_type_common
*event_context_type
= NULL
;
231 struct bt_field_type_common
*event_payload_type
= NULL
;
232 const enum bt_validation_flag validation_flags
=
233 BT_VALIDATION_FLAG_EVENT
;
234 struct bt_clock_class
*expected_clock_class
= NULL
;
236 BT_ASSERT(copy_field_type_func
);
238 if (!stream_class
|| !event_class
) {
239 BT_LOGW("Invalid parameter: stream class or event class is NULL: "
240 "stream-class-addr=%p, event-class-addr=%p",
241 stream_class
, event_class
);
246 BT_LOGD("Adding event class to stream class: "
247 "stream-class-addr=%p, stream-class-name=\"%s\", "
248 "stream-class-id=%" PRId64
", event-class-addr=%p, "
249 "event-class-name=\"%s\", event-class-id=%" PRId64
,
250 stream_class
, bt_stream_class_common_get_name(stream_class
),
251 bt_stream_class_common_get_id(stream_class
),
253 bt_event_class_common_get_name(event_class
),
254 bt_event_class_common_get_id(event_class
));
256 trace
= bt_stream_class_common_get_trace(stream_class
);
258 if (stream_class
->frozen
) {
260 * We only check that the event class to be added has a
261 * single class which matches the stream class's
262 * expected clock class if the stream class is frozen.
263 * If it's not, then this event class is added "as is"
264 * and the validation will be performed when calling
265 * either bt_trace_add_stream_class() or
266 * bt_event_create(). This is because the stream class's
267 * field types (packet context, event header, event
268 * context) could change before the next call to one of
269 * those two functions.
271 expected_clock_class
= bt_get(stream_class
->clock_class
);
274 * At this point, `expected_clock_class` can be NULL,
275 * and bt_event_class_validate_single_clock_class()
278 ret
= bt_event_class_common_validate_single_clock_class(
279 event_class
, &expected_clock_class
);
281 BT_LOGW("Event class contains a field type which is not "
282 "recursively mapped to its stream class's "
283 "expected clock class: "
284 "stream-class-addr=%p, "
285 "stream-class-id=%" PRId64
", "
286 "stream-class-name=\"%s\", "
287 "expected-clock-class-addr=%p, "
288 "expected-clock-class-name=\"%s\"",
290 bt_stream_class_common_get_id(stream_class
),
291 bt_stream_class_common_get_name(stream_class
),
292 expected_clock_class
,
293 expected_clock_class
?
294 bt_clock_class_get_name(expected_clock_class
) :
300 event_id
= g_new(int64_t, 1);
302 BT_LOGE_STR("Failed to allocate one int64_t.");
307 /* Check for duplicate event classes */
308 struct search_query query
= { .value
= event_class
, .found
= 0 };
309 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
312 BT_LOGW_STR("Another event class part of this stream class has the same ID.");
317 old_stream_class
= bt_event_class_common_borrow_stream_class(event_class
);
318 if (old_stream_class
) {
319 /* Event class is already associated to a stream class. */
320 BT_LOGW("Event class is already part of another stream class: "
321 "event-class-stream-class-addr=%p, "
322 "event-class-stream-class-name=\"%s\", "
323 "event-class-stream-class-id=%" PRId64
,
325 bt_stream_class_common_get_name(old_stream_class
),
326 bt_stream_class_common_get_id(old_stream_class
));
333 * If the stream class is associated with a trace, then
334 * both those objects are frozen. Also, this event class
335 * is about to be frozen.
337 * Therefore the event class must be validated here.
338 * The trace and stream class should be valid at this
341 BT_ASSERT(trace
->valid
);
342 BT_ASSERT(stream_class
->valid
);
344 bt_trace_common_get_packet_header_field_type(trace
);
345 packet_context_type
=
346 bt_stream_class_common_get_packet_context_field_type(
349 bt_stream_class_common_get_event_header_field_type(
351 stream_event_ctx_type
=
352 bt_stream_class_common_get_event_context_field_type(
355 bt_event_class_common_get_context_field_type(event_class
);
357 bt_event_class_common_get_payload_field_type(event_class
);
358 ret
= bt_validate_class_types(
359 trace
->environment
, packet_header_type
,
360 packet_context_type
, event_header_type
,
361 stream_event_ctx_type
, event_context_type
,
362 event_payload_type
, trace
->valid
,
363 stream_class
->valid
, event_class
->valid
,
364 &validation_output
, validation_flags
,
365 copy_field_type_func
);
366 BT_PUT(packet_header_type
);
367 BT_PUT(packet_context_type
);
368 BT_PUT(event_header_type
);
369 BT_PUT(stream_event_ctx_type
);
370 BT_PUT(event_context_type
);
371 BT_PUT(event_payload_type
);
375 * This means something went wrong during the
376 * validation process, not that the objects are
379 BT_LOGE("Failed to validate event class: ret=%d", ret
);
383 if ((validation_output
.valid_flags
& validation_flags
) !=
385 /* Invalid event class */
386 BT_LOGW("Invalid trace, stream class, or event class: "
388 validation_output
.valid_flags
);
394 /* Only set an event ID if none was explicitly set before */
395 *event_id
= bt_event_class_common_get_id(event_class
);
397 BT_LOGV("Event class has no ID: automatically setting it: "
398 "id=%" PRId64
, stream_class
->next_event_id
);
400 if (bt_event_class_common_set_id(event_class
,
401 stream_class
->next_event_id
)) {
402 BT_LOGE("Cannot set event class's ID: id=%" PRId64
,
403 stream_class
->next_event_id
);
407 stream_class
->next_event_id
++;
408 *event_id
= stream_class
->next_event_id
;
411 bt_object_set_parent(event_class
, stream_class
);
415 * At this point we know that the function will be
416 * successful. Therefore we can replace the event
417 * class's field types with what's in the validation
418 * output structure and mark this event class as valid.
420 bt_validation_replace_types(NULL
, NULL
, event_class
,
421 &validation_output
, validation_flags
);
422 event_class
->valid
= 1;
425 * Put what was not moved in
426 * bt_validation_replace_types().
428 bt_validation_output_put_types(&validation_output
);
431 /* Add to the event classes of the stream class */
432 g_ptr_array_add(stream_class
->event_classes
, event_class
);
433 g_hash_table_insert(stream_class
->event_classes_ht
, event_id
,
437 /* Freeze the event class */
438 bt_event_class_common_freeze(event_class
);
441 * It is safe to set the stream class's unique clock class
442 * now if the stream class is frozen.
444 if (stream_class
->frozen
&& expected_clock_class
) {
445 BT_ASSERT(!stream_class
->clock_class
||
446 stream_class
->clock_class
== expected_clock_class
);
447 BT_MOVE(stream_class
->clock_class
, expected_clock_class
);
450 BT_LOGD("Added event class to stream class: "
451 "stream-class-addr=%p, stream-class-name=\"%s\", "
452 "stream-class-id=%" PRId64
", event-class-addr=%p, "
453 "event-class-name=\"%s\", event-class-id=%" PRId64
,
454 stream_class
, bt_stream_class_common_get_name(stream_class
),
455 bt_stream_class_common_get_id(stream_class
),
457 bt_event_class_common_get_name(event_class
),
458 bt_event_class_common_get_id(event_class
));
462 bt_validation_output_put_types(&validation_output
);
463 bt_put(expected_clock_class
);
464 BT_ASSERT(!packet_header_type
);
465 BT_ASSERT(!packet_context_type
);
466 BT_ASSERT(!event_header_type
);
467 BT_ASSERT(!stream_event_ctx_type
);
468 BT_ASSERT(!event_context_type
);
469 BT_ASSERT(!event_payload_type
);
474 int bt_stream_class_add_event_class(
475 struct bt_stream_class
*stream_class
,
476 struct bt_event_class
*event_class
)
478 struct bt_trace
*trace
;
482 BT_LOGW("Invalid parameter: stream class is NULL: "
483 "stream-class-addr=%p", stream_class
);
488 trace
= BT_FROM_COMMON(bt_stream_class_common_borrow_trace(
489 BT_TO_COMMON(stream_class
)));
490 if (trace
&& trace
->is_static
) {
491 BT_LOGW("Invalid parameter: stream class's trace is static: "
492 "trace-addr=%p, trace-name=\"%s\"",
493 trace
, bt_trace_get_name(trace
));
498 ret
= bt_stream_class_common_add_event_class(
499 BT_TO_COMMON(stream_class
), BT_TO_COMMON(event_class
),
500 (bt_validation_flag_copy_field_type_func
) bt_field_type_copy
);
505 /* Notifiy listeners of the trace's schema modification. */
507 struct bt_visitor_object obj
= { .object
= event_class
,
508 .type
= BT_VISITOR_OBJECT_TYPE_EVENT_CLASS
};
510 (void) bt_trace_object_modification(&obj
, trace
);
517 int64_t bt_stream_class_get_event_class_count(
518 struct bt_stream_class
*stream_class
)
520 return bt_stream_class_common_get_event_class_count(
521 BT_TO_COMMON(stream_class
));
524 struct bt_event_class
*bt_stream_class_get_event_class_by_index(
525 struct bt_stream_class
*stream_class
, uint64_t index
)
527 return BT_FROM_COMMON(bt_stream_class_common_get_event_class_by_index(
528 BT_TO_COMMON(stream_class
), index
));
531 struct bt_event_class
*bt_stream_class_get_event_class_by_id(
532 struct bt_stream_class
*stream_class
, uint64_t id
)
534 return BT_FROM_COMMON(bt_stream_class_common_get_event_class_by_id(
535 BT_TO_COMMON(stream_class
), id
));
538 struct bt_field_type
*bt_stream_class_get_packet_context_field_type(
539 struct bt_stream_class
*stream_class
)
541 return BT_FROM_COMMON(bt_stream_class_common_get_packet_context_field_type(
542 BT_TO_COMMON(stream_class
)));
545 int bt_stream_class_set_packet_context_field_type(
546 struct bt_stream_class
*stream_class
,
547 struct bt_field_type
*packet_context_type
)
549 return bt_stream_class_common_set_packet_context_field_type(
550 BT_TO_COMMON(stream_class
), (void *) packet_context_type
);
553 struct bt_field_type
*bt_stream_class_get_event_header_field_type(
554 struct bt_stream_class
*stream_class
)
556 return BT_FROM_COMMON(bt_stream_class_common_get_event_header_field_type(
557 BT_TO_COMMON(stream_class
)));
560 int bt_stream_class_set_event_header_field_type(
561 struct bt_stream_class
*stream_class
,
562 struct bt_field_type
*event_header_type
)
564 return bt_stream_class_common_set_event_header_field_type(
565 BT_TO_COMMON(stream_class
), (void *) event_header_type
);
568 struct bt_field_type
*bt_stream_class_get_event_context_field_type(
569 struct bt_stream_class
*stream_class
)
571 return BT_FROM_COMMON(bt_stream_class_common_get_event_context_field_type(
572 BT_TO_COMMON(stream_class
)));
575 int bt_stream_class_set_event_context_field_type(
576 struct bt_stream_class
*stream_class
,
577 struct bt_field_type
*event_context_type
)
579 return bt_stream_class_common_set_event_context_field_type(
580 BT_TO_COMMON(stream_class
), (void *) event_context_type
);
584 int64_t get_event_class_count(void *element
)
586 return bt_stream_class_get_event_class_count(
587 (struct bt_stream_class
*) element
);
591 void *get_event_class(void *element
, int i
)
593 return bt_stream_class_get_event_class_by_index(
594 (struct bt_stream_class
*) element
, i
);
598 int visit_event_class(void *object
, bt_visitor visitor
,void *data
)
600 struct bt_visitor_object obj
= {
602 .type
= BT_VISITOR_OBJECT_TYPE_EVENT_CLASS
605 return visitor(&obj
, data
);
609 int bt_stream_class_common_visit(struct bt_stream_class_common
*stream_class
,
610 bt_visitor visitor
, void *data
)
613 struct bt_visitor_object obj
= {
614 .object
= stream_class
,
615 .type
= BT_VISITOR_OBJECT_TYPE_STREAM_CLASS
618 if (!stream_class
|| !visitor
) {
619 BT_LOGW("Invalid parameter: stream class or visitor is NULL: "
620 "stream-class-addr=%p, visitor=%p",
621 stream_class
, visitor
);
626 ret
= visitor_helper(&obj
, get_event_class_count
,
628 visit_event_class
, visitor
, data
);
629 BT_LOGV("visitor_helper() returned: ret=%d", ret
);
635 int bt_stream_class_visit(struct bt_stream_class
*stream_class
,
636 bt_visitor visitor
, void *data
)
638 return bt_stream_class_common_visit(BT_FROM_COMMON(stream_class
),
643 void bt_stream_class_common_freeze(struct bt_stream_class_common
*stream_class
)
645 if (!stream_class
|| stream_class
->frozen
) {
649 BT_LOGD("Freezing stream class: addr=%p, name=\"%s\", id=%" PRId64
,
650 stream_class
, bt_stream_class_common_get_name(stream_class
),
651 bt_stream_class_common_get_id(stream_class
));
652 stream_class
->frozen
= 1;
653 bt_field_type_common_freeze(stream_class
->event_header_field_type
);
654 bt_field_type_common_freeze(stream_class
->packet_context_field_type
);
655 bt_field_type_common_freeze(stream_class
->event_context_field_type
);
656 bt_clock_class_freeze(stream_class
->clock_class
);
659 void bt_stream_class_freeze(struct bt_stream_class
*stream_class
)
661 bt_stream_class_common_freeze(BT_TO_COMMON(stream_class
));
665 int bt_stream_class_common_validate_single_clock_class(
666 struct bt_stream_class_common
*stream_class
,
667 struct bt_clock_class
**expected_clock_class
)
672 BT_ASSERT(stream_class
);
673 BT_ASSERT(expected_clock_class
);
674 ret
= bt_field_type_common_validate_single_clock_class(
675 stream_class
->packet_context_field_type
,
676 expected_clock_class
);
678 BT_LOGW("Stream class's packet context field type "
679 "is not recursively mapped to the "
680 "expected clock class: "
681 "stream-class-addr=%p, "
682 "stream-class-name=\"%s\", "
683 "stream-class-id=%" PRId64
", "
686 bt_stream_class_common_get_name(stream_class
),
688 stream_class
->packet_context_field_type
);
692 ret
= bt_field_type_common_validate_single_clock_class(
693 stream_class
->event_header_field_type
,
694 expected_clock_class
);
696 BT_LOGW("Stream class's event header field type "
697 "is not recursively mapped to the "
698 "expected clock class: "
699 "stream-class-addr=%p, "
700 "stream-class-name=\"%s\", "
701 "stream-class-id=%" PRId64
", "
704 bt_stream_class_common_get_name(stream_class
),
706 stream_class
->event_header_field_type
);
710 ret
= bt_field_type_common_validate_single_clock_class(
711 stream_class
->event_context_field_type
,
712 expected_clock_class
);
714 BT_LOGW("Stream class's event context field type "
715 "is not recursively mapped to the "
716 "expected clock class: "
717 "stream-class-addr=%p, "
718 "stream-class-name=\"%s\", "
719 "stream-class-id=%" PRId64
", "
722 bt_stream_class_common_get_name(stream_class
),
724 stream_class
->event_context_field_type
);
728 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
729 struct bt_event_class_common
*event_class
=
730 g_ptr_array_index(stream_class
->event_classes
, i
);
732 BT_ASSERT(event_class
);
733 ret
= bt_event_class_common_validate_single_clock_class(
734 event_class
, expected_clock_class
);
736 BT_LOGW("Stream class's event class contains a "
737 "field type which is not recursively mapped to "
738 "the expected clock class: "
739 "stream-class-addr=%p, "
740 "stream-class-name=\"%s\", "
741 "stream-class-id=%" PRId64
,
743 bt_stream_class_common_get_name(stream_class
),