2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/STREAM-CLASS"
9 #include "lib/logging.h"
11 #include "lib/assert-cond.h"
12 #include <babeltrace2/trace-ir/trace.h>
13 #include "compat/compiler.h"
14 #include "common/align.h"
15 #include "compat/endian.h"
16 #include "common/assert.h"
17 #include "lib/property.h"
22 #include "clock-class.h"
23 #include "event-class.h"
24 #include "field-class.h"
26 #include "field-wrapper.h"
27 #include "resolve-field-path.h"
28 #include "stream-class.h"
31 #include "lib/value.h"
32 #include "lib/func-status.h"
34 #define BT_ASSERT_PRE_DEV_STREAM_CLASS_HOT(_sc) \
35 BT_ASSERT_PRE_DEV_HOT((_sc), "Stream class", ": %!+S", (_sc))
38 void destroy_stream_class(struct bt_object
*obj
)
40 struct bt_stream_class
*stream_class
= (void *) obj
;
42 BT_LIB_LOGD("Destroying stream class: %!+S", stream_class
);
43 BT_LOGD_STR("Putting default clock class.");
44 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->user_attributes
);
45 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->default_clock_class
);
47 if (stream_class
->event_classes
) {
48 BT_LOGD_STR("Destroying event classes.");
49 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
50 stream_class
->event_classes
= NULL
;
53 if (stream_class
->name
.str
) {
54 g_string_free(stream_class
->name
.str
, TRUE
);
55 stream_class
->name
.str
= NULL
;
56 stream_class
->name
.value
= NULL
;
59 BT_LOGD_STR("Putting packet context field class.");
60 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->packet_context_fc
);
61 BT_LOGD_STR("Putting event common context field class.");
62 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->event_common_context_fc
);
63 bt_object_pool_finalize(&stream_class
->packet_context_field_pool
);
68 void free_field_wrapper(struct bt_field_wrapper
*field_wrapper
,
69 struct bt_stream_class
*stream_class
)
71 bt_field_wrapper_destroy((void *) field_wrapper
);
75 bool stream_class_id_is_unique(const struct bt_trace_class
*tc
, uint64_t id
)
78 bool is_unique
= true;
80 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
81 const struct bt_stream_class
*sc
=
82 tc
->stream_classes
->pdata
[i
];
95 struct bt_stream_class
*create_stream_class_with_id(
96 struct bt_trace_class
*tc
, uint64_t id
)
98 struct bt_stream_class
*stream_class
= NULL
;
102 BT_ASSERT_PRE(stream_class_id_is_unique(tc
, id
),
103 "Duplicate stream class ID: %![tc-]+T, id=%" PRIu64
, tc
, id
);
104 BT_LIB_LOGD("Creating stream class object: %![tc-]+T, id=%" PRIu64
,
106 stream_class
= g_new0(struct bt_stream_class
, 1);
108 BT_LIB_LOGE_APPEND_CAUSE(
109 "Failed to allocate one stream class.");
113 bt_object_init_shared_with_parent(&stream_class
->base
,
114 destroy_stream_class
);
115 stream_class
->user_attributes
= bt_value_map_create();
116 if (!stream_class
->user_attributes
) {
117 BT_LIB_LOGE_APPEND_CAUSE(
118 "Failed to create a map value object.");
122 stream_class
->name
.str
= g_string_new(NULL
);
123 if (!stream_class
->name
.str
) {
124 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
128 stream_class
->id
= id
;
129 stream_class
->assigns_automatic_event_class_id
= true;
130 stream_class
->assigns_automatic_stream_id
= true;
131 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
132 (GDestroyNotify
) bt_object_try_spec_release
);
133 if (!stream_class
->event_classes
) {
134 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
138 ret
= bt_object_pool_initialize(&stream_class
->packet_context_field_pool
,
139 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
140 (bt_object_pool_destroy_object_func
) free_field_wrapper
,
143 BT_LIB_LOGE_APPEND_CAUSE(
144 "Failed to initialize packet context field pool: ret=%d",
149 bt_object_set_parent(&stream_class
->base
, &tc
->base
);
150 g_ptr_array_add(tc
->stream_classes
, stream_class
);
151 bt_trace_class_freeze(tc
);
152 BT_LIB_LOGD("Created stream class object: %!+S", stream_class
);
156 BT_OBJECT_PUT_REF_AND_RESET(stream_class
);
162 struct bt_stream_class
*bt_stream_class_create(struct bt_trace_class
*tc
)
164 BT_ASSERT_PRE_NO_ERROR();
165 BT_ASSERT_PRE_TC_NON_NULL(tc
);
166 BT_ASSERT_PRE(tc
->assigns_automatic_stream_class_id
,
167 "Trace class does not automatically assigns stream class IDs: "
169 return create_stream_class_with_id(tc
,
170 (uint64_t) tc
->stream_classes
->len
);
173 struct bt_stream_class
*bt_stream_class_create_with_id(
174 struct bt_trace_class
*tc
, uint64_t id
)
176 BT_ASSERT_PRE_NO_ERROR();
177 BT_ASSERT_PRE_TC_NON_NULL(tc
);
178 BT_ASSERT_PRE(!tc
->assigns_automatic_stream_class_id
,
179 "Trace class automatically assigns stream class IDs: "
181 return create_stream_class_with_id(tc
, id
);
184 struct bt_trace_class
*bt_stream_class_borrow_trace_class(
185 struct bt_stream_class
*stream_class
)
187 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
188 return bt_stream_class_borrow_trace_class_inline(stream_class
);
191 const struct bt_trace_class
*bt_stream_class_borrow_trace_class_const(
192 const struct bt_stream_class
*stream_class
)
194 return bt_stream_class_borrow_trace_class((void *) stream_class
);
197 const char *bt_stream_class_get_name(const struct bt_stream_class
*stream_class
)
199 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
200 return stream_class
->name
.value
;
203 enum bt_stream_class_set_name_status
bt_stream_class_set_name(
204 struct bt_stream_class
*stream_class
,
207 BT_ASSERT_PRE_NO_ERROR();
208 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
209 BT_ASSERT_PRE_NAME_NON_NULL(name
);
210 BT_ASSERT_PRE_DEV_STREAM_CLASS_HOT(stream_class
);
211 g_string_assign(stream_class
->name
.str
, name
);
212 stream_class
->name
.value
= stream_class
->name
.str
->str
;
213 BT_LIB_LOGD("Set stream class's name: %!+S", stream_class
);
214 return BT_FUNC_STATUS_OK
;
217 uint64_t bt_stream_class_get_id(const struct bt_stream_class
*stream_class
)
219 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
220 return stream_class
->id
;
223 uint64_t bt_stream_class_get_event_class_count(
224 const struct bt_stream_class
*stream_class
)
226 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
227 return (uint64_t) stream_class
->event_classes
->len
;
230 struct bt_event_class
*bt_stream_class_borrow_event_class_by_index(
231 struct bt_stream_class
*stream_class
, uint64_t index
)
233 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
234 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, stream_class
->event_classes
->len
);
235 return g_ptr_array_index(stream_class
->event_classes
, index
);
238 const struct bt_event_class
*
239 bt_stream_class_borrow_event_class_by_index_const(
240 const struct bt_stream_class
*stream_class
, uint64_t index
)
242 return bt_stream_class_borrow_event_class_by_index(
243 (void *) stream_class
, index
);
246 struct bt_event_class
*bt_stream_class_borrow_event_class_by_id(
247 struct bt_stream_class
*stream_class
, uint64_t id
)
249 struct bt_event_class
*event_class
= NULL
;
252 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
254 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
255 struct bt_event_class
*event_class_candidate
=
256 g_ptr_array_index(stream_class
->event_classes
, i
);
258 if (event_class_candidate
->id
== id
) {
259 event_class
= event_class_candidate
;
268 const struct bt_event_class
*
269 bt_stream_class_borrow_event_class_by_id_const(
270 const struct bt_stream_class
*stream_class
, uint64_t id
)
272 return bt_stream_class_borrow_event_class_by_id(
273 (void *) stream_class
, id
);
276 const struct bt_field_class
*
277 bt_stream_class_borrow_packet_context_field_class_const(
278 const struct bt_stream_class
*stream_class
)
280 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
281 return stream_class
->packet_context_fc
;
284 struct bt_field_class
*
285 bt_stream_class_borrow_packet_context_field_class(
286 struct bt_stream_class
*stream_class
)
288 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
289 return stream_class
->packet_context_fc
;
292 enum bt_stream_class_set_field_class_status
293 bt_stream_class_set_packet_context_field_class(
294 struct bt_stream_class
*stream_class
,
295 struct bt_field_class
*field_class
)
298 struct bt_resolve_field_path_context resolve_ctx
= {
299 .packet_context
= field_class
,
300 .event_common_context
= NULL
,
301 .event_specific_context
= NULL
,
302 .event_payload
= NULL
,
305 BT_ASSERT_PRE_NO_ERROR();
306 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
307 BT_ASSERT_PRE(stream_class
->supports_packets
,
308 "Stream class does not support packets: %![sc-]+S",
310 BT_ASSERT_PRE_FC_NON_NULL(field_class
);
311 BT_ASSERT_PRE_DEV_STREAM_CLASS_HOT(stream_class
);
312 BT_ASSERT_PRE_FC_IS_STRUCT(field_class
, "Packet context field class");
313 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
316 * This is the only reason for which
317 * bt_resolve_field_paths() can fail: anything else
318 * would be because a precondition is not satisfied.
320 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
324 bt_field_class_make_part_of_trace_class(field_class
);
325 bt_object_put_ref(stream_class
->packet_context_fc
);
326 stream_class
->packet_context_fc
= field_class
;
327 bt_object_get_ref_no_null_check(stream_class
->packet_context_fc
);
328 bt_field_class_freeze(field_class
);
329 BT_LIB_LOGD("Set stream class's packet context field class: %!+S",
336 const struct bt_field_class
*
337 bt_stream_class_borrow_event_common_context_field_class_const(
338 const struct bt_stream_class
*stream_class
)
340 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
341 return stream_class
->event_common_context_fc
;
344 struct bt_field_class
*
345 bt_stream_class_borrow_event_common_context_field_class(
346 struct bt_stream_class
*stream_class
)
348 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
349 return stream_class
->event_common_context_fc
;
352 enum bt_stream_class_set_field_class_status
353 bt_stream_class_set_event_common_context_field_class(
354 struct bt_stream_class
*stream_class
,
355 struct bt_field_class
*field_class
)
358 struct bt_resolve_field_path_context resolve_ctx
= {
359 .packet_context
= NULL
,
360 .event_common_context
= field_class
,
361 .event_specific_context
= NULL
,
362 .event_payload
= NULL
,
365 BT_ASSERT_PRE_NO_ERROR();
366 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
367 BT_ASSERT_PRE_FC_NON_NULL(field_class
);
368 BT_ASSERT_PRE_DEV_STREAM_CLASS_HOT(stream_class
);
369 BT_ASSERT_PRE_FC_IS_STRUCT(field_class
,
370 "Event common context field class");
371 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
372 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
375 * This is the only reason for which
376 * bt_resolve_field_paths() can fail: anything else
377 * would be because a precondition is not satisfied.
379 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
383 bt_field_class_make_part_of_trace_class(field_class
);
384 bt_object_put_ref(stream_class
->event_common_context_fc
);
385 stream_class
->event_common_context_fc
= field_class
;
386 bt_object_get_ref_no_null_check(stream_class
->event_common_context_fc
);
387 bt_field_class_freeze(field_class
);
388 BT_LIB_LOGD("Set stream class's event common context field class: %!+S",
396 void _bt_stream_class_freeze(const struct bt_stream_class
*stream_class
)
398 /* The field classes and default clock class are already frozen */
399 BT_ASSERT(stream_class
);
400 BT_LIB_LOGD("Freezing stream class's user attributes: %!+v",
401 stream_class
->user_attributes
);
402 bt_value_freeze(stream_class
->user_attributes
);
403 BT_LIB_LOGD("Freezing stream class: %!+S", stream_class
);
404 ((struct bt_stream_class
*) stream_class
)->frozen
= true;
407 enum bt_stream_class_set_default_clock_class_status
408 bt_stream_class_set_default_clock_class(
409 struct bt_stream_class
*stream_class
,
410 struct bt_clock_class
*clock_class
)
412 BT_ASSERT_PRE_NO_ERROR();
413 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
414 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
415 BT_ASSERT_PRE_DEV_STREAM_CLASS_HOT(stream_class
);
416 bt_object_put_ref(stream_class
->default_clock_class
);
417 stream_class
->default_clock_class
= clock_class
;
418 bt_object_get_ref_no_null_check(stream_class
->default_clock_class
);
419 bt_clock_class_freeze(clock_class
);
420 BT_LIB_LOGD("Set stream class's default clock class: %!+S",
422 return BT_FUNC_STATUS_OK
;
425 struct bt_clock_class
*bt_stream_class_borrow_default_clock_class(
426 struct bt_stream_class
*stream_class
)
428 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
429 return stream_class
->default_clock_class
;
432 const struct bt_clock_class
*bt_stream_class_borrow_default_clock_class_const(
433 const struct bt_stream_class
*stream_class
)
435 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
436 return stream_class
->default_clock_class
;
439 bt_bool
bt_stream_class_assigns_automatic_event_class_id(
440 const struct bt_stream_class
*stream_class
)
442 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
443 return (bt_bool
) stream_class
->assigns_automatic_event_class_id
;
446 void bt_stream_class_set_assigns_automatic_event_class_id(
447 struct bt_stream_class
*stream_class
,
450 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
451 BT_ASSERT_PRE_DEV_STREAM_CLASS_HOT(stream_class
);
452 stream_class
->assigns_automatic_event_class_id
= (bool) value
;
453 BT_LIB_LOGD("Set stream class's automatic event class ID "
454 "assignment property: %!+S", stream_class
);
457 bt_bool
bt_stream_class_assigns_automatic_stream_id(
458 const struct bt_stream_class
*stream_class
)
460 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
461 return (bt_bool
) stream_class
->assigns_automatic_stream_id
;
464 void bt_stream_class_set_supports_discarded_events(
465 struct bt_stream_class
*stream_class
,
466 bt_bool supports_discarded_events
,
467 bt_bool with_default_clock_snapshots
)
469 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
470 BT_ASSERT_PRE_DEV_STREAM_CLASS_HOT(stream_class
);
471 BT_ASSERT_PRE(supports_discarded_events
||
472 !with_default_clock_snapshots
,
473 "Discarded events cannot have default clock snapshots when "
474 "not supported: %!+S", stream_class
);
475 BT_ASSERT_PRE(!with_default_clock_snapshots
||
476 stream_class
->default_clock_class
,
477 "Stream class has no default clock class: %!+S", stream_class
);
478 stream_class
->supports_discarded_events
=
479 (bool) supports_discarded_events
;
480 stream_class
->discarded_events_have_default_clock_snapshots
=
481 (bool) with_default_clock_snapshots
;
482 BT_LIB_LOGD("Set stream class's discarded events support property: "
483 "%!+S", stream_class
);
486 bt_bool
bt_stream_class_supports_discarded_events(
487 const struct bt_stream_class
*stream_class
)
489 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
490 return (bt_bool
) stream_class
->supports_discarded_events
;
493 bt_bool
bt_stream_class_discarded_events_have_default_clock_snapshots(
494 const struct bt_stream_class
*stream_class
)
496 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
497 return (bt_bool
) stream_class
->discarded_events_have_default_clock_snapshots
;
500 void bt_stream_class_set_supports_discarded_packets(
501 struct bt_stream_class
*stream_class
,
502 bt_bool supports_discarded_packets
,
503 bt_bool with_default_clock_snapshots
)
505 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
506 BT_ASSERT_PRE_DEV_STREAM_CLASS_HOT(stream_class
);
507 BT_ASSERT_PRE(!supports_discarded_packets
||
508 stream_class
->supports_packets
,
509 "Stream class does not support packets: %!+S",
511 BT_ASSERT_PRE(supports_discarded_packets
||
512 !with_default_clock_snapshots
,
513 "Discarded packets cannot have default clock snapshots when "
514 "not supported: %!+S", stream_class
);
515 BT_ASSERT_PRE(!with_default_clock_snapshots
||
516 stream_class
->default_clock_class
,
517 "Stream class has no default clock class: %!+S", stream_class
);
518 stream_class
->supports_discarded_packets
=
519 (bool) supports_discarded_packets
;
520 stream_class
->discarded_packets_have_default_clock_snapshots
=
521 (bool) with_default_clock_snapshots
;
522 BT_LIB_LOGD("Set stream class's discarded packets support property: "
523 "%!+S", stream_class
);
526 bt_bool
bt_stream_class_supports_discarded_packets(
527 const struct bt_stream_class
*stream_class
)
529 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
530 return (bt_bool
) stream_class
->supports_discarded_packets
;
533 bt_bool
bt_stream_class_discarded_packets_have_default_clock_snapshots(
534 const struct bt_stream_class
*stream_class
)
536 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
537 return (bt_bool
) stream_class
->discarded_packets_have_default_clock_snapshots
;
540 void bt_stream_class_set_supports_packets(
541 struct bt_stream_class
*stream_class
,
542 bt_bool supports_packets
,
543 bt_bool with_beginning_default_clock_snapshot
,
544 bt_bool with_end_default_clock_snapshot
)
546 bt_bool with_default_clock_snapshot
=
547 with_beginning_default_clock_snapshot
||
548 with_end_default_clock_snapshot
;
549 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
550 BT_ASSERT_PRE_DEV_STREAM_CLASS_HOT(stream_class
);
551 BT_ASSERT_PRE(supports_packets
||
552 !with_default_clock_snapshot
,
553 "Packets cannot have default clock snapshots when "
554 "not supported: %!+S", stream_class
);
555 BT_ASSERT_PRE(!with_default_clock_snapshot
||
556 stream_class
->default_clock_class
,
557 "Stream class has no default clock class: %!+S", stream_class
);
558 BT_ASSERT_PRE(supports_packets
|| !stream_class
->packet_context_fc
,
559 "Stream class already has a packet context field class: %!+S",
561 BT_ASSERT_PRE(supports_packets
||
562 !stream_class
->supports_discarded_packets
,
563 "Stream class already supports discarded packets: %!+S",
565 stream_class
->supports_packets
= (bool) supports_packets
;
566 stream_class
->packets_have_beginning_default_clock_snapshot
=
567 (bool) with_beginning_default_clock_snapshot
;
568 stream_class
->packets_have_end_default_clock_snapshot
=
569 (bool) with_end_default_clock_snapshot
;
570 BT_LIB_LOGD("Set stream class's packets support property: %!+S",
574 bt_bool
bt_stream_class_supports_packets(
575 const struct bt_stream_class
*stream_class
)
577 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
578 return (bt_bool
) stream_class
->supports_packets
;
581 bt_bool
bt_stream_class_packets_have_beginning_default_clock_snapshot(
582 const struct bt_stream_class
*stream_class
)
584 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
585 return (bt_bool
) stream_class
->packets_have_beginning_default_clock_snapshot
;
588 bt_bool
bt_stream_class_packets_have_end_default_clock_snapshot(
589 const struct bt_stream_class
*stream_class
)
591 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
592 return (bt_bool
) stream_class
->packets_have_end_default_clock_snapshot
;
595 void bt_stream_class_set_assigns_automatic_stream_id(
596 struct bt_stream_class
*stream_class
,
599 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
600 BT_ASSERT_PRE_DEV_STREAM_CLASS_HOT(stream_class
);
601 stream_class
->assigns_automatic_stream_id
= (bool) value
;
602 BT_LIB_LOGD("Set stream class's automatic stream ID "
603 "assignment property: %!+S", stream_class
);
606 const struct bt_value
*bt_stream_class_borrow_user_attributes_const(
607 const struct bt_stream_class
*stream_class
)
609 BT_ASSERT_PRE_DEV_SC_NON_NULL(stream_class
);
610 return stream_class
->user_attributes
;
613 struct bt_value
*bt_stream_class_borrow_user_attributes(
614 struct bt_stream_class
*stream_class
)
616 return (void *) bt_stream_class_borrow_user_attributes_const(
617 (void *) stream_class
);
620 void bt_stream_class_set_user_attributes(
621 struct bt_stream_class
*stream_class
,
622 const struct bt_value
*user_attributes
)
624 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
625 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
626 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
627 BT_ASSERT_PRE_DEV_STREAM_CLASS_HOT(stream_class
);
628 bt_object_put_ref_no_null_check(stream_class
->user_attributes
);
629 stream_class
->user_attributes
= (void *) user_attributes
;
630 bt_object_get_ref_no_null_check(stream_class
->user_attributes
);
633 void bt_stream_class_get_ref(const struct bt_stream_class
*stream_class
)
635 bt_object_get_ref(stream_class
);
638 void bt_stream_class_put_ref(const struct bt_stream_class
*stream_class
)
640 bt_object_put_ref(stream_class
);