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-classes-internal.h>
32 #include <babeltrace/trace-ir/fields-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 classe.");
74 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->event_header_fc
);
75 BT_LOGD_STR("Putting packet context field classe.");
76 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->packet_context_fc
);
77 BT_LOGD_STR("Putting event common context field classe.");
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
*trace
, uint64_t id
)
96 bool is_unique
= true;
98 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
99 const struct bt_stream_class
*sc
=
100 trace
->stream_classes
->pdata
[i
];
113 struct bt_stream_class
*create_stream_class_with_id(struct bt_trace
*trace
,
116 struct bt_stream_class
*stream_class
= NULL
;
120 BT_ASSERT_PRE(stream_class_id_is_unique(trace
, id
),
121 "Duplicate stream class ID: %![trace-]+t, id=%" PRIu64
,
123 BT_LIB_LOGD("Creating stream class object: %![trace-]+t, id=%" PRIu64
,
125 stream_class
= g_new0(struct bt_stream_class
, 1);
127 BT_LOGE_STR("Failed to allocate one stream class.");
131 bt_object_init_shared_with_parent(&stream_class
->base
,
132 destroy_stream_class
);
134 stream_class
->name
.str
= g_string_new(NULL
);
135 if (!stream_class
->name
.str
) {
136 BT_LOGE_STR("Failed to allocate a GString.");
141 stream_class
->id
= id
;
142 stream_class
->assigns_automatic_event_class_id
= true;
143 stream_class
->assigns_automatic_stream_id
= true;
144 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
145 (GDestroyNotify
) bt_object_try_spec_release
);
146 if (!stream_class
->event_classes
) {
147 BT_LOGE_STR("Failed to allocate a GPtrArray.");
151 ret
= bt_object_pool_initialize(&stream_class
->event_header_field_pool
,
152 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
153 (bt_object_pool_destroy_object_func
) free_field_wrapper
,
156 BT_LOGE("Failed to initialize event header field pool: ret=%d",
161 ret
= bt_object_pool_initialize(&stream_class
->packet_context_field_pool
,
162 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
163 (bt_object_pool_destroy_object_func
) free_field_wrapper
,
166 BT_LOGE("Failed to initialize packet context field pool: ret=%d",
171 bt_object_set_parent(&stream_class
->base
, &trace
->base
);
172 g_ptr_array_add(trace
->stream_classes
, stream_class
);
173 bt_trace_freeze(trace
);
174 BT_LIB_LOGD("Created stream class object: %!+S", stream_class
);
178 BT_OBJECT_PUT_REF_AND_RESET(stream_class
);
184 struct bt_stream_class
*bt_stream_class_create(struct bt_trace
*trace
)
186 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
187 BT_ASSERT_PRE(trace
->assigns_automatic_stream_class_id
,
188 "Trace does not automatically assigns stream class IDs: "
190 return create_stream_class_with_id(trace
,
191 (uint64_t) trace
->stream_classes
->len
);
194 struct bt_stream_class
*bt_stream_class_create_with_id(
195 struct bt_trace
*trace
, uint64_t id
)
197 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
198 BT_ASSERT_PRE(!trace
->assigns_automatic_stream_class_id
,
199 "Trace automatically assigns stream class IDs: "
201 return create_stream_class_with_id(trace
, id
);
204 struct bt_trace
*bt_stream_class_borrow_trace(
205 struct bt_stream_class
*stream_class
)
207 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
208 return bt_stream_class_borrow_trace_inline(stream_class
);
211 const struct bt_trace
*bt_stream_class_borrow_trace_const(
212 const struct bt_stream_class
*stream_class
)
214 return bt_stream_class_borrow_trace((void *) stream_class
);
217 const char *bt_stream_class_get_name(const struct bt_stream_class
*stream_class
)
219 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
220 return stream_class
->name
.value
;
223 int bt_stream_class_set_name(
224 struct bt_stream_class
*stream_class
,
227 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
228 BT_ASSERT_PRE_NON_NULL(name
, "Name");
229 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
230 g_string_assign(stream_class
->name
.str
, name
);
231 stream_class
->name
.value
= stream_class
->name
.str
->str
;
232 BT_LIB_LOGV("Set stream class's name: %!+S", stream_class
);
236 uint64_t bt_stream_class_get_id(const struct bt_stream_class
*stream_class
)
238 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
239 return stream_class
->id
;
242 uint64_t bt_stream_class_get_event_class_count(
243 const struct bt_stream_class
*stream_class
)
245 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
246 return (uint64_t) stream_class
->event_classes
->len
;
249 struct bt_event_class
*bt_stream_class_borrow_event_class_by_index(
250 struct bt_stream_class
*stream_class
, uint64_t index
)
252 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
253 BT_ASSERT_PRE_VALID_INDEX(index
, stream_class
->event_classes
->len
);
254 return g_ptr_array_index(stream_class
->event_classes
, index
);
257 const struct bt_event_class
*
258 bt_stream_class_borrow_event_class_by_index_const(
259 const struct bt_stream_class
*stream_class
, uint64_t index
)
261 return bt_stream_class_borrow_event_class_by_index(
262 (void *) stream_class
, index
);
265 struct bt_event_class
*bt_stream_class_borrow_event_class_by_id(
266 struct bt_stream_class
*stream_class
, uint64_t id
)
268 struct bt_event_class
*event_class
= NULL
;
271 BT_ASSERT_PRE_NON_NULL(stream_class
, "Trace");
273 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
274 struct bt_event_class
*event_class_candidate
=
275 g_ptr_array_index(stream_class
->event_classes
, i
);
277 if (event_class_candidate
->id
== id
) {
278 event_class
= event_class_candidate
;
287 const struct bt_event_class
*
288 bt_stream_class_borrow_event_class_by_id_const(
289 const struct bt_stream_class
*stream_class
, uint64_t id
)
291 return bt_stream_class_borrow_event_class_by_id(
292 (void *) stream_class
, id
);
295 const struct bt_field_class
*
296 bt_stream_class_borrow_packet_context_field_class_const(
297 const struct bt_stream_class
*stream_class
)
299 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
300 return stream_class
->packet_context_fc
;
303 int bt_stream_class_set_packet_context_field_class(
304 struct bt_stream_class
*stream_class
,
305 struct bt_field_class
*field_class
)
308 struct bt_resolve_field_path_context resolve_ctx
= {
309 .packet_header
= NULL
,
310 .packet_context
= field_class
,
311 .event_header
= NULL
,
312 .event_common_context
= NULL
,
313 .event_specific_context
= NULL
,
314 .event_payload
= NULL
,
317 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
318 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
319 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
320 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
321 BT_FIELD_CLASS_TYPE_STRUCTURE
,
322 "Packet context field classe is not a structure field classe: %!+F",
324 resolve_ctx
.packet_header
=
325 bt_stream_class_borrow_trace_inline(stream_class
)->packet_header_fc
;
326 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
331 bt_field_class_make_part_of_trace(field_class
);
332 bt_object_put_ref(stream_class
->packet_context_fc
);
333 stream_class
->packet_context_fc
= field_class
;
334 bt_object_get_no_null_check(stream_class
->packet_context_fc
);
335 bt_field_class_freeze(field_class
);
336 BT_LIB_LOGV("Set stream class's packet context field classe: %!+S",
343 const struct bt_field_class
*bt_stream_class_borrow_event_header_field_class_const(
344 const struct bt_stream_class
*stream_class
)
346 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
347 return stream_class
->event_header_fc
;
350 int bt_stream_class_set_event_header_field_class(
351 struct bt_stream_class
*stream_class
,
352 struct bt_field_class
*field_class
)
355 struct bt_resolve_field_path_context resolve_ctx
= {
356 .packet_header
= NULL
,
357 .packet_context
= NULL
,
358 .event_header
= field_class
,
359 .event_common_context
= NULL
,
360 .event_specific_context
= NULL
,
361 .event_payload
= NULL
,
364 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
365 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
366 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
367 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
368 BT_FIELD_CLASS_TYPE_STRUCTURE
,
369 "Event header field classe is not a structure field classe: %!+F",
371 resolve_ctx
.packet_header
=
372 bt_stream_class_borrow_trace_inline(stream_class
)->packet_header_fc
;
373 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
374 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
379 bt_field_class_make_part_of_trace(field_class
);
380 bt_object_put_ref(stream_class
->event_header_fc
);
381 stream_class
->event_header_fc
= field_class
;
382 bt_object_get_no_null_check(stream_class
->event_header_fc
);
383 bt_field_class_freeze(field_class
);
384 BT_LIB_LOGV("Set stream class's event header field classe: %!+S",
391 const struct bt_field_class
*
392 bt_stream_class_borrow_event_common_context_field_class_const(
393 const struct bt_stream_class
*stream_class
)
395 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
396 return stream_class
->event_common_context_fc
;
399 int bt_stream_class_set_event_common_context_field_class(
400 struct bt_stream_class
*stream_class
,
401 struct bt_field_class
*field_class
)
404 struct bt_resolve_field_path_context resolve_ctx
= {
405 .packet_header
= NULL
,
406 .packet_context
= NULL
,
407 .event_header
= NULL
,
408 .event_common_context
= field_class
,
409 .event_specific_context
= NULL
,
410 .event_payload
= NULL
,
413 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
414 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
415 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
416 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
417 BT_FIELD_CLASS_TYPE_STRUCTURE
,
418 "Event common context field classe is not a structure field classe: %!+F",
420 resolve_ctx
.packet_header
=
421 bt_stream_class_borrow_trace_inline(stream_class
)->packet_header_fc
;
422 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
423 resolve_ctx
.event_header
= stream_class
->event_header_fc
;
424 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
429 bt_field_class_make_part_of_trace(field_class
);
430 bt_object_put_ref(stream_class
->event_common_context_fc
);
431 stream_class
->event_common_context_fc
= field_class
;
432 bt_object_get_no_null_check(stream_class
->event_common_context_fc
);
433 bt_field_class_freeze(field_class
);
434 BT_LIB_LOGV("Set stream class's event common context field classe: %!+S",
442 void _bt_stream_class_freeze(const struct bt_stream_class
*stream_class
)
444 /* The field classes and default clock class are already frozen */
445 BT_ASSERT(stream_class
);
446 BT_LIB_LOGD("Freezing stream class: %!+S", stream_class
);
447 ((struct bt_stream_class
*) stream_class
)->frozen
= true;
450 int bt_stream_class_set_default_clock_class(
451 struct bt_stream_class
*stream_class
,
452 struct bt_clock_class
*clock_class
)
454 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
455 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
456 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
457 bt_object_put_ref(stream_class
->default_clock_class
);
458 stream_class
->default_clock_class
= clock_class
;
459 bt_object_get_no_null_check(stream_class
->default_clock_class
);
460 bt_clock_class_freeze(clock_class
);
461 BT_LIB_LOGV("Set stream class's default clock class: %!+S",
466 struct bt_clock_class
*bt_stream_class_borrow_default_clock_class(
467 struct bt_stream_class
*stream_class
)
469 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
470 return stream_class
->default_clock_class
;
473 const struct bt_clock_class
*bt_stream_class_borrow_default_clock_class_const(
474 const struct bt_stream_class
*stream_class
)
476 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
477 return stream_class
->default_clock_class
;
480 bt_bool
bt_stream_class_assigns_automatic_event_class_id(
481 const struct bt_stream_class
*stream_class
)
483 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
484 return (bt_bool
) stream_class
->assigns_automatic_event_class_id
;
487 void bt_stream_class_set_assigns_automatic_event_class_id(
488 struct bt_stream_class
*stream_class
,
491 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
492 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
493 stream_class
->assigns_automatic_event_class_id
= (bool) value
;
494 BT_LIB_LOGV("Set stream class's automatic event class ID "
495 "assignment property: %!+S", stream_class
);
498 bt_bool
bt_stream_class_assigns_automatic_stream_id(
499 const struct bt_stream_class
*stream_class
)
501 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
502 return (bt_bool
) stream_class
->assigns_automatic_stream_id
;
505 void bt_stream_class_set_assigns_automatic_stream_id(
506 struct bt_stream_class
*stream_class
,
509 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
510 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
511 stream_class
->assigns_automatic_stream_id
= (bool) value
;
512 BT_LIB_LOGV("Set stream class's automatic stream ID "
513 "assignment property: %!+S", stream_class
);
516 bt_bool
bt_stream_class_packets_have_discarded_event_counter_snapshot(
517 const struct bt_stream_class
*stream_class
)
519 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
520 return (bt_bool
) stream_class
->packets_have_discarded_event_counter_snapshot
;
523 void bt_stream_class_set_packets_have_discarded_event_counter_snapshot(
524 struct bt_stream_class
*stream_class
,
527 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
528 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
529 stream_class
->packets_have_discarded_event_counter_snapshot
=
531 BT_LIB_LOGV("Set stream class's "
532 "\"packets have discarded event counter snapshot\" property: "
533 "%!+S", stream_class
);
536 bt_bool
bt_stream_class_packets_have_packet_counter_snapshot(
537 const struct bt_stream_class
*stream_class
)
539 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
540 return (bt_bool
) stream_class
->packets_have_packet_counter_snapshot
;
543 void bt_stream_class_set_packets_have_packet_counter_snapshot(
544 struct bt_stream_class
*stream_class
,
547 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
548 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
549 stream_class
->packets_have_packet_counter_snapshot
=
551 BT_LIB_LOGV("Set stream class's "
552 "\"packets have packet counter snapshot\" property: "
553 "%!+S", stream_class
);
556 bt_bool
bt_stream_class_packets_have_default_beginning_clock_value(
557 const struct bt_stream_class
*stream_class
)
559 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
560 return (bt_bool
) stream_class
->packets_have_default_beginning_cv
;
563 void bt_stream_class_set_packets_have_default_beginning_clock_value(
564 struct bt_stream_class
*stream_class
,
567 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
568 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
569 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
570 "Stream class does not have a default clock class: %!+S",
572 stream_class
->packets_have_default_beginning_cv
= (bool) value
;
573 BT_LIB_LOGV("Set stream class's "
574 "\"packets have default beginning clock value\" property: "
575 "%!+S", stream_class
);
578 bt_bool
bt_stream_class_packets_have_default_end_clock_value(
579 const struct bt_stream_class
*stream_class
)
581 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
582 return (bt_bool
) stream_class
->packets_have_default_end_cv
;
585 void bt_stream_class_set_packets_have_default_end_clock_value(
586 struct bt_stream_class
*stream_class
,
589 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
590 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
591 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
592 "Stream class does not have a default clock class: %!+S",
594 stream_class
->packets_have_default_end_cv
= (bool) value
;
595 BT_LIB_LOGV("Set stream class's "
596 "\"packets have default end clock value\" property: "
597 "%!+S", stream_class
);
600 bt_bool
bt_stream_class_default_clock_is_always_known(
601 const struct bt_stream_class
*stream_class
)
603 /* BT_CLOCK_VALUE_STATUS_UNKNOWN is not supported as of 2.0 */