2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "STREAM-CLASS"
26 #include <babeltrace/lib-logging-internal.h>
28 #include <babeltrace/assert-pre-internal.h>
29 #include <babeltrace/trace-ir/clock-class-internal.h>
30 #include <babeltrace/trace-ir/event-class-internal.h>
31 #include <babeltrace/trace-ir/field-class-internal.h>
32 #include <babeltrace/trace-ir/field-internal.h>
33 #include <babeltrace/trace-ir/stream-class-internal.h>
34 #include <babeltrace/trace-ir/trace-const.h>
35 #include <babeltrace/trace-ir/trace-internal.h>
36 #include <babeltrace/trace-ir/utils-internal.h>
37 #include <babeltrace/trace-ir/field-wrapper-internal.h>
38 #include <babeltrace/trace-ir/resolve-field-path-internal.h>
39 #include <babeltrace/object.h>
40 #include <babeltrace/compiler-internal.h>
41 #include <babeltrace/align-internal.h>
42 #include <babeltrace/endian-internal.h>
43 #include <babeltrace/assert-internal.h>
44 #include <babeltrace/property-internal.h>
49 #define BT_ASSERT_PRE_STREAM_CLASS_HOT(_sc) \
50 BT_ASSERT_PRE_HOT((_sc), "Stream class", ": %!+S", (_sc))
53 void destroy_stream_class(struct bt_object
*obj
)
55 struct bt_stream_class
*stream_class
= (void *) obj
;
57 BT_LIB_LOGD("Destroying stream class: %!+S", stream_class
);
58 BT_LOGD_STR("Putting default clock class.");
59 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->default_clock_class
);
61 if (stream_class
->event_classes
) {
62 BT_LOGD_STR("Destroying event classes.");
63 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
64 stream_class
->event_classes
= NULL
;
67 if (stream_class
->name
.str
) {
68 g_string_free(stream_class
->name
.str
, TRUE
);
69 stream_class
->name
.str
= NULL
;
70 stream_class
->name
.value
= NULL
;
73 BT_LOGD_STR("Putting event header field class.");
74 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->event_header_fc
);
75 BT_LOGD_STR("Putting packet context field class.");
76 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->packet_context_fc
);
77 BT_LOGD_STR("Putting event common context field class.");
78 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->event_common_context_fc
);
79 bt_object_pool_finalize(&stream_class
->event_header_field_pool
);
80 bt_object_pool_finalize(&stream_class
->packet_context_field_pool
);
85 void free_field_wrapper(struct bt_field_wrapper
*field_wrapper
,
86 struct bt_stream_class
*stream_class
)
88 bt_field_wrapper_destroy((void *) field_wrapper
);
93 bool stream_class_id_is_unique(const struct bt_trace_class
*tc
, uint64_t id
)
96 bool is_unique
= true;
98 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
99 const struct bt_stream_class
*sc
=
100 tc
->stream_classes
->pdata
[i
];
113 struct bt_stream_class
*create_stream_class_with_id(
114 struct bt_trace_class
*tc
, uint64_t id
)
116 struct bt_stream_class
*stream_class
= NULL
;
120 BT_ASSERT_PRE(stream_class_id_is_unique(tc
, id
),
121 "Duplicate stream class ID: %![tc-]+T, id=%" PRIu64
, tc
, id
);
122 BT_LIB_LOGD("Creating stream class object: %![tc-]+T, id=%" PRIu64
,
124 stream_class
= g_new0(struct bt_stream_class
, 1);
126 BT_LOGE_STR("Failed to allocate one stream class.");
130 bt_object_init_shared_with_parent(&stream_class
->base
,
131 destroy_stream_class
);
133 stream_class
->name
.str
= g_string_new(NULL
);
134 if (!stream_class
->name
.str
) {
135 BT_LOGE_STR("Failed to allocate a GString.");
140 stream_class
->id
= id
;
141 stream_class
->assigns_automatic_event_class_id
= true;
142 stream_class
->assigns_automatic_stream_id
= true;
143 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
144 (GDestroyNotify
) bt_object_try_spec_release
);
145 if (!stream_class
->event_classes
) {
146 BT_LOGE_STR("Failed to allocate a GPtrArray.");
150 ret
= bt_object_pool_initialize(&stream_class
->event_header_field_pool
,
151 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
152 (bt_object_pool_destroy_object_func
) free_field_wrapper
,
155 BT_LOGE("Failed to initialize event header field pool: ret=%d",
160 ret
= bt_object_pool_initialize(&stream_class
->packet_context_field_pool
,
161 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
162 (bt_object_pool_destroy_object_func
) free_field_wrapper
,
165 BT_LOGE("Failed to initialize packet context field pool: ret=%d",
170 bt_object_set_parent(&stream_class
->base
, &tc
->base
);
171 g_ptr_array_add(tc
->stream_classes
, stream_class
);
172 bt_trace_class_freeze(tc
);
173 BT_LIB_LOGD("Created stream class object: %!+S", stream_class
);
177 BT_OBJECT_PUT_REF_AND_RESET(stream_class
);
183 struct bt_stream_class
*bt_stream_class_create(struct bt_trace_class
*tc
)
185 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
186 BT_ASSERT_PRE(tc
->assigns_automatic_stream_class_id
,
187 "Trace class does not automatically assigns stream class IDs: "
189 return create_stream_class_with_id(tc
,
190 (uint64_t) tc
->stream_classes
->len
);
193 struct bt_stream_class
*bt_stream_class_create_with_id(
194 struct bt_trace_class
*tc
, uint64_t id
)
196 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
197 BT_ASSERT_PRE(!tc
->assigns_automatic_stream_class_id
,
198 "Trace class automatically assigns stream class IDs: "
200 return create_stream_class_with_id(tc
, id
);
203 struct bt_trace_class
*bt_stream_class_borrow_trace_class(
204 struct bt_stream_class
*stream_class
)
206 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
207 return bt_stream_class_borrow_trace_class_inline(stream_class
);
210 const struct bt_trace_class
*bt_stream_class_borrow_trace_class_const(
211 const struct bt_stream_class
*stream_class
)
213 return bt_stream_class_borrow_trace_class((void *) stream_class
);
216 const char *bt_stream_class_get_name(const struct bt_stream_class
*stream_class
)
218 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
219 return stream_class
->name
.value
;
222 int bt_stream_class_set_name(
223 struct bt_stream_class
*stream_class
,
226 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
227 BT_ASSERT_PRE_NON_NULL(name
, "Name");
228 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
229 g_string_assign(stream_class
->name
.str
, name
);
230 stream_class
->name
.value
= stream_class
->name
.str
->str
;
231 BT_LIB_LOGV("Set stream class's name: %!+S", stream_class
);
235 uint64_t bt_stream_class_get_id(const struct bt_stream_class
*stream_class
)
237 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
238 return stream_class
->id
;
241 uint64_t bt_stream_class_get_event_class_count(
242 const struct bt_stream_class
*stream_class
)
244 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
245 return (uint64_t) stream_class
->event_classes
->len
;
248 struct bt_event_class
*bt_stream_class_borrow_event_class_by_index(
249 struct bt_stream_class
*stream_class
, uint64_t index
)
251 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
252 BT_ASSERT_PRE_VALID_INDEX(index
, stream_class
->event_classes
->len
);
253 return g_ptr_array_index(stream_class
->event_classes
, index
);
256 const struct bt_event_class
*
257 bt_stream_class_borrow_event_class_by_index_const(
258 const struct bt_stream_class
*stream_class
, uint64_t index
)
260 return bt_stream_class_borrow_event_class_by_index(
261 (void *) stream_class
, index
);
264 struct bt_event_class
*bt_stream_class_borrow_event_class_by_id(
265 struct bt_stream_class
*stream_class
, uint64_t id
)
267 struct bt_event_class
*event_class
= NULL
;
270 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
272 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
273 struct bt_event_class
*event_class_candidate
=
274 g_ptr_array_index(stream_class
->event_classes
, i
);
276 if (event_class_candidate
->id
== id
) {
277 event_class
= event_class_candidate
;
286 const struct bt_event_class
*
287 bt_stream_class_borrow_event_class_by_id_const(
288 const struct bt_stream_class
*stream_class
, uint64_t id
)
290 return bt_stream_class_borrow_event_class_by_id(
291 (void *) stream_class
, id
);
294 const struct bt_field_class
*
295 bt_stream_class_borrow_packet_context_field_class_const(
296 const struct bt_stream_class
*stream_class
)
298 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
299 return stream_class
->packet_context_fc
;
302 int bt_stream_class_set_packet_context_field_class(
303 struct bt_stream_class
*stream_class
,
304 struct bt_field_class
*field_class
)
307 struct bt_resolve_field_path_context resolve_ctx
= {
308 .packet_header
= NULL
,
309 .packet_context
= field_class
,
310 .event_header
= NULL
,
311 .event_common_context
= NULL
,
312 .event_specific_context
= NULL
,
313 .event_payload
= NULL
,
316 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
317 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
318 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
319 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
320 BT_FIELD_CLASS_TYPE_STRUCTURE
,
321 "Packet context field class is not a structure field class: %!+F",
323 resolve_ctx
.packet_header
=
324 bt_stream_class_borrow_trace_class_inline(stream_class
)->packet_header_fc
;
325 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
330 bt_field_class_make_part_of_trace_class(field_class
);
331 bt_object_put_ref(stream_class
->packet_context_fc
);
332 stream_class
->packet_context_fc
= field_class
;
333 bt_object_get_no_null_check(stream_class
->packet_context_fc
);
334 bt_field_class_freeze(field_class
);
335 BT_LIB_LOGV("Set stream class's packet context field class: %!+S",
342 const struct bt_field_class
*bt_stream_class_borrow_event_header_field_class_const(
343 const struct bt_stream_class
*stream_class
)
345 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
346 return stream_class
->event_header_fc
;
349 int bt_stream_class_set_event_header_field_class(
350 struct bt_stream_class
*stream_class
,
351 struct bt_field_class
*field_class
)
354 struct bt_resolve_field_path_context resolve_ctx
= {
355 .packet_header
= NULL
,
356 .packet_context
= NULL
,
357 .event_header
= field_class
,
358 .event_common_context
= NULL
,
359 .event_specific_context
= NULL
,
360 .event_payload
= NULL
,
363 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
364 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
365 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
366 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
367 BT_FIELD_CLASS_TYPE_STRUCTURE
,
368 "Event header field class is not a structure field class: %!+F",
370 resolve_ctx
.packet_header
=
371 bt_stream_class_borrow_trace_class_inline(stream_class
)->packet_header_fc
;
372 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
373 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
378 bt_field_class_make_part_of_trace_class(field_class
);
379 bt_object_put_ref(stream_class
->event_header_fc
);
380 stream_class
->event_header_fc
= field_class
;
381 bt_object_get_no_null_check(stream_class
->event_header_fc
);
382 bt_field_class_freeze(field_class
);
383 BT_LIB_LOGV("Set stream class's event header field class: %!+S",
390 const struct bt_field_class
*
391 bt_stream_class_borrow_event_common_context_field_class_const(
392 const struct bt_stream_class
*stream_class
)
394 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
395 return stream_class
->event_common_context_fc
;
398 int bt_stream_class_set_event_common_context_field_class(
399 struct bt_stream_class
*stream_class
,
400 struct bt_field_class
*field_class
)
403 struct bt_resolve_field_path_context resolve_ctx
= {
404 .packet_header
= NULL
,
405 .packet_context
= NULL
,
406 .event_header
= NULL
,
407 .event_common_context
= field_class
,
408 .event_specific_context
= NULL
,
409 .event_payload
= NULL
,
412 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
413 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
414 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
415 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
416 BT_FIELD_CLASS_TYPE_STRUCTURE
,
417 "Event common context field class is not a structure field class: %!+F",
419 resolve_ctx
.packet_header
=
420 bt_stream_class_borrow_trace_class_inline(stream_class
)->packet_header_fc
;
421 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
422 resolve_ctx
.event_header
= stream_class
->event_header_fc
;
423 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
428 bt_field_class_make_part_of_trace_class(field_class
);
429 bt_object_put_ref(stream_class
->event_common_context_fc
);
430 stream_class
->event_common_context_fc
= field_class
;
431 bt_object_get_no_null_check(stream_class
->event_common_context_fc
);
432 bt_field_class_freeze(field_class
);
433 BT_LIB_LOGV("Set stream class's event common context field class: %!+S",
441 void _bt_stream_class_freeze(const struct bt_stream_class
*stream_class
)
443 /* The field classes and default clock class are already frozen */
444 BT_ASSERT(stream_class
);
445 BT_LIB_LOGD("Freezing stream class: %!+S", stream_class
);
446 ((struct bt_stream_class
*) stream_class
)->frozen
= true;
449 int bt_stream_class_set_default_clock_class(
450 struct bt_stream_class
*stream_class
,
451 struct bt_clock_class
*clock_class
)
453 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
454 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
455 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
456 bt_object_put_ref(stream_class
->default_clock_class
);
457 stream_class
->default_clock_class
= clock_class
;
458 bt_object_get_no_null_check(stream_class
->default_clock_class
);
459 bt_clock_class_freeze(clock_class
);
460 BT_LIB_LOGV("Set stream class's default clock class: %!+S",
465 struct bt_clock_class
*bt_stream_class_borrow_default_clock_class(
466 struct bt_stream_class
*stream_class
)
468 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
469 return stream_class
->default_clock_class
;
472 const struct bt_clock_class
*bt_stream_class_borrow_default_clock_class_const(
473 const struct bt_stream_class
*stream_class
)
475 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
476 return stream_class
->default_clock_class
;
479 bt_bool
bt_stream_class_assigns_automatic_event_class_id(
480 const struct bt_stream_class
*stream_class
)
482 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
483 return (bt_bool
) stream_class
->assigns_automatic_event_class_id
;
486 void bt_stream_class_set_assigns_automatic_event_class_id(
487 struct bt_stream_class
*stream_class
,
490 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
491 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
492 stream_class
->assigns_automatic_event_class_id
= (bool) value
;
493 BT_LIB_LOGV("Set stream class's automatic event class ID "
494 "assignment property: %!+S", stream_class
);
497 bt_bool
bt_stream_class_assigns_automatic_stream_id(
498 const struct bt_stream_class
*stream_class
)
500 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
501 return (bt_bool
) stream_class
->assigns_automatic_stream_id
;
504 void bt_stream_class_set_assigns_automatic_stream_id(
505 struct bt_stream_class
*stream_class
,
508 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
509 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
510 stream_class
->assigns_automatic_stream_id
= (bool) value
;
511 BT_LIB_LOGV("Set stream class's automatic stream ID "
512 "assignment property: %!+S", stream_class
);
515 bt_bool
bt_stream_class_packets_have_discarded_event_counter_snapshot(
516 const struct bt_stream_class
*stream_class
)
518 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
519 return (bt_bool
) stream_class
->packets_have_discarded_event_counter_snapshot
;
522 void bt_stream_class_set_packets_have_discarded_event_counter_snapshot(
523 struct bt_stream_class
*stream_class
,
526 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
527 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
528 stream_class
->packets_have_discarded_event_counter_snapshot
=
530 BT_LIB_LOGV("Set stream class's "
531 "\"packets have discarded event counter snapshot\" property: "
532 "%!+S", stream_class
);
535 bt_bool
bt_stream_class_packets_have_packet_counter_snapshot(
536 const struct bt_stream_class
*stream_class
)
538 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
539 return (bt_bool
) stream_class
->packets_have_packet_counter_snapshot
;
542 void bt_stream_class_set_packets_have_packet_counter_snapshot(
543 struct bt_stream_class
*stream_class
,
546 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
547 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
548 stream_class
->packets_have_packet_counter_snapshot
=
550 BT_LIB_LOGV("Set stream class's "
551 "\"packets have packet counter snapshot\" property: "
552 "%!+S", stream_class
);
555 bt_bool
bt_stream_class_packets_have_default_beginning_clock_value(
556 const struct bt_stream_class
*stream_class
)
558 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
559 return (bt_bool
) stream_class
->packets_have_default_beginning_cv
;
562 void bt_stream_class_set_packets_have_default_beginning_clock_value(
563 struct bt_stream_class
*stream_class
,
566 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
567 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
568 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
569 "Stream class does not have a default clock class: %!+S",
571 stream_class
->packets_have_default_beginning_cv
= (bool) value
;
572 BT_LIB_LOGV("Set stream class's "
573 "\"packets have default beginning clock value\" property: "
574 "%!+S", stream_class
);
577 bt_bool
bt_stream_class_packets_have_default_end_clock_value(
578 const struct bt_stream_class
*stream_class
)
580 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
581 return (bt_bool
) stream_class
->packets_have_default_end_cv
;
584 void bt_stream_class_set_packets_have_default_end_clock_value(
585 struct bt_stream_class
*stream_class
,
588 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
589 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
590 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
591 "Stream class does not have a default clock class: %!+S",
593 stream_class
->packets_have_default_end_cv
= (bool) value
;
594 BT_LIB_LOGV("Set stream class's "
595 "\"packets have default end clock value\" property: "
596 "%!+S", stream_class
);
599 bt_bool
bt_stream_class_default_clock_is_always_known(
600 const struct bt_stream_class
*stream_class
)
602 /* BT_CLOCK_VALUE_STATUS_UNKNOWN is not supported as of 2.0 */