4 * Babeltrace trace 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/trace-ir/clock-class-internal.h>
34 #include <babeltrace/trace-ir/event-class-internal.h>
35 #include <babeltrace/trace-ir/field-types-internal.h>
36 #include <babeltrace/trace-ir/fields-internal.h>
37 #include <babeltrace/trace-ir/stream-class-internal.h>
38 #include <babeltrace/trace-ir/utils-internal.h>
39 #include <babeltrace/trace-ir/field-wrapper-internal.h>
40 #include <babeltrace/trace-ir/resolve-field-path-internal.h>
41 #include <babeltrace/ref.h>
42 #include <babeltrace/compiler-internal.h>
43 #include <babeltrace/align-internal.h>
44 #include <babeltrace/endian-internal.h>
45 #include <babeltrace/assert-internal.h>
46 #include <babeltrace/property-internal.h>
51 #define BT_ASSERT_PRE_STREAM_CLASS_HOT(_sc) \
52 BT_ASSERT_PRE_HOT((_sc), "Stream class", ": %!+S", (_sc))
55 void destroy_stream_class(struct bt_object
*obj
)
57 struct bt_stream_class
*stream_class
= (void *) obj
;
59 BT_LIB_LOGD("Destroying stream class: %!+S", stream_class
);
60 BT_LOGD_STR("Putting default clock class.");
61 bt_put(stream_class
->default_clock_class
);
63 if (stream_class
->event_classes
) {
64 BT_LOGD_STR("Destroying event classes.");
65 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
68 if (stream_class
->name
.str
) {
69 g_string_free(stream_class
->name
.str
, TRUE
);
72 BT_LOGD_STR("Putting event header field type.");
73 bt_put(stream_class
->event_header_ft
);
74 BT_LOGD_STR("Putting packet context field type.");
75 bt_put(stream_class
->packet_context_ft
);
76 BT_LOGD_STR("Putting event common context field type.");
77 bt_put(stream_class
->event_common_context_ft
);
78 bt_object_pool_finalize(&stream_class
->event_header_field_pool
);
79 bt_object_pool_finalize(&stream_class
->packet_context_field_pool
);
84 void free_field_wrapper(struct bt_field_wrapper
*field_wrapper
,
85 struct bt_stream_class
*stream_class
)
87 bt_field_wrapper_destroy((void *) field_wrapper
);
92 bool stream_class_id_is_unique(struct bt_trace
*trace
, uint64_t id
)
95 bool is_unique
= true;
97 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
98 struct bt_stream_class
*sc
=
99 trace
->stream_classes
->pdata
[i
];
112 struct bt_stream_class
*create_stream_class_with_id(struct bt_trace
*trace
,
115 struct bt_stream_class
*stream_class
= NULL
;
119 BT_ASSERT_PRE(stream_class_id_is_unique(trace
, id
),
120 "Duplicate stream class ID: %![trace-]+t, id=%" PRIu64
,
122 BT_LIB_LOGD("Creating stream class object: %![trace-]+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
, &trace
->base
);
171 g_ptr_array_add(trace
->stream_classes
, stream_class
);
172 bt_trace_freeze(trace
);
173 BT_LIB_LOGD("Created stream class object: %!+S", stream_class
);
177 BT_PUT(stream_class
);
183 struct bt_stream_class
*bt_stream_class_create(struct bt_trace
*trace
)
185 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
186 BT_ASSERT_PRE(trace
->assigns_automatic_stream_class_id
,
187 "Trace does not automatically assigns stream class IDs: "
189 return create_stream_class_with_id(trace
,
190 (uint64_t) trace
->stream_classes
->len
);
193 struct bt_stream_class
*bt_stream_class_create_with_id(
194 struct bt_trace
*trace
, uint64_t id
)
196 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
197 BT_ASSERT_PRE(!trace
->assigns_automatic_stream_class_id
,
198 "Trace automatically assigns stream class IDs: "
200 return create_stream_class_with_id(trace
, id
);
203 struct bt_trace
*bt_stream_class_borrow_trace(struct bt_stream_class
*stream_class
)
205 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
206 return bt_stream_class_borrow_trace_inline(stream_class
);
209 const char *bt_stream_class_get_name(struct bt_stream_class
*stream_class
)
211 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
212 return stream_class
->name
.value
;
215 int bt_stream_class_set_name(struct bt_stream_class
*stream_class
,
218 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
219 BT_ASSERT_PRE_NON_NULL(name
, "Name");
220 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
221 g_string_assign(stream_class
->name
.str
, name
);
222 stream_class
->name
.value
= stream_class
->name
.str
->str
;
223 BT_LIB_LOGV("Set stream class's name: %!+S", stream_class
);
227 uint64_t bt_stream_class_get_id(struct bt_stream_class
*stream_class
)
229 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
230 return stream_class
->id
;
233 uint64_t bt_stream_class_get_event_class_count(
234 struct bt_stream_class
*stream_class
)
236 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
237 return (uint64_t) stream_class
->event_classes
->len
;
240 struct bt_event_class
*bt_stream_class_borrow_event_class_by_index(
241 struct bt_stream_class
*stream_class
, uint64_t index
)
243 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
244 BT_ASSERT_PRE_VALID_INDEX(index
, stream_class
->event_classes
->len
);
245 return g_ptr_array_index(stream_class
->event_classes
, index
);
248 struct bt_event_class
*bt_stream_class_borrow_event_class_by_id(
249 struct bt_stream_class
*trace
, uint64_t id
)
251 struct bt_event_class
*event_class
= NULL
;
254 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
256 for (i
= 0; i
< trace
->event_classes
->len
; i
++) {
257 struct bt_event_class
*event_class_candidate
=
258 g_ptr_array_index(trace
->event_classes
, i
);
260 if (event_class_candidate
->id
== id
) {
261 event_class
= event_class_candidate
;
270 struct bt_field_type
*bt_stream_class_borrow_packet_context_field_type(
271 struct bt_stream_class
*stream_class
)
273 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
274 return stream_class
->packet_context_ft
;
277 int bt_stream_class_set_packet_context_field_type(
278 struct bt_stream_class
*stream_class
,
279 struct bt_field_type
*field_type
)
282 struct bt_resolve_field_path_context resolve_ctx
= {
283 .packet_header
= NULL
,
284 .packet_context
= field_type
,
285 .event_header
= NULL
,
286 .event_common_context
= NULL
,
287 .event_specific_context
= NULL
,
288 .event_payload
= NULL
,
291 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
292 BT_ASSERT_PRE_NON_NULL(field_type
, "Field type");
293 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
294 BT_ASSERT_PRE(bt_field_type_get_type_id(field_type
) ==
295 BT_FIELD_TYPE_ID_STRUCTURE
,
296 "Packet context field type is not a structure field type: %!+F",
298 resolve_ctx
.packet_header
=
299 bt_stream_class_borrow_trace_inline(stream_class
)->packet_header_ft
;
300 ret
= bt_resolve_field_paths(field_type
, &resolve_ctx
);
305 bt_field_type_make_part_of_trace(field_type
);
306 bt_put(stream_class
->packet_context_ft
);
307 stream_class
->packet_context_ft
= bt_get(field_type
);
308 bt_field_type_freeze(field_type
);
309 BT_LIB_LOGV("Set stream class's packet context field type: %!+S",
316 struct bt_field_type
*bt_stream_class_borrow_event_header_field_type(
317 struct bt_stream_class
*stream_class
)
319 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
320 return stream_class
->event_header_ft
;
323 int bt_stream_class_set_event_header_field_type(
324 struct bt_stream_class
*stream_class
,
325 struct bt_field_type
*field_type
)
328 struct bt_resolve_field_path_context resolve_ctx
= {
329 .packet_header
= NULL
,
330 .packet_context
= NULL
,
331 .event_header
= field_type
,
332 .event_common_context
= NULL
,
333 .event_specific_context
= NULL
,
334 .event_payload
= NULL
,
337 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
338 BT_ASSERT_PRE_NON_NULL(field_type
, "Field type");
339 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
340 BT_ASSERT_PRE(bt_field_type_get_type_id(field_type
) ==
341 BT_FIELD_TYPE_ID_STRUCTURE
,
342 "Event header field type is not a structure field type: %!+F",
344 resolve_ctx
.packet_header
=
345 bt_stream_class_borrow_trace_inline(stream_class
)->packet_header_ft
;
346 resolve_ctx
.packet_context
= stream_class
->packet_context_ft
;
347 ret
= bt_resolve_field_paths(field_type
, &resolve_ctx
);
352 bt_field_type_make_part_of_trace(field_type
);
353 bt_put(stream_class
->event_header_ft
);
354 stream_class
->event_header_ft
= bt_get(field_type
);
355 bt_field_type_freeze(field_type
);
356 BT_LIB_LOGV("Set stream class's event header field type: %!+S",
363 struct bt_field_type
*bt_stream_class_borrow_event_common_context_field_type(
364 struct bt_stream_class
*stream_class
)
366 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
367 return stream_class
->event_common_context_ft
;
370 int bt_stream_class_set_event_common_context_field_type(
371 struct bt_stream_class
*stream_class
,
372 struct bt_field_type
*field_type
)
375 struct bt_resolve_field_path_context resolve_ctx
= {
376 .packet_header
= NULL
,
377 .packet_context
= NULL
,
378 .event_header
= NULL
,
379 .event_common_context
= field_type
,
380 .event_specific_context
= NULL
,
381 .event_payload
= NULL
,
384 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
385 BT_ASSERT_PRE_NON_NULL(field_type
, "Field type");
386 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
387 BT_ASSERT_PRE(bt_field_type_get_type_id(field_type
) ==
388 BT_FIELD_TYPE_ID_STRUCTURE
,
389 "Event common context field type is not a structure field type: %!+F",
391 resolve_ctx
.packet_header
=
392 bt_stream_class_borrow_trace_inline(stream_class
)->packet_header_ft
;
393 resolve_ctx
.packet_context
= stream_class
->packet_context_ft
;
394 resolve_ctx
.event_header
= stream_class
->event_header_ft
;
395 ret
= bt_resolve_field_paths(field_type
, &resolve_ctx
);
400 bt_field_type_make_part_of_trace(field_type
);
401 bt_put(stream_class
->event_common_context_ft
);
402 stream_class
->event_common_context_ft
= bt_get(field_type
);
403 bt_field_type_freeze(field_type
);
404 BT_LIB_LOGV("Set stream class's event common context field type: %!+S",
412 void _bt_stream_class_freeze(struct bt_stream_class
*stream_class
)
414 /* The field types and default clock class are already frozen */
415 BT_ASSERT(stream_class
);
416 BT_LIB_LOGD("Freezing stream class: %!+S", stream_class
);
417 stream_class
->frozen
= true;
420 int bt_stream_class_set_default_clock_class(
421 struct bt_stream_class
*stream_class
,
422 struct bt_clock_class
*clock_class
)
424 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
425 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
426 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
427 bt_put(stream_class
->default_clock_class
);
428 stream_class
->default_clock_class
= bt_get(clock_class
);
429 bt_clock_class_freeze(clock_class
);
430 BT_LIB_LOGV("Set stream class's default clock class: %!+S",
435 struct bt_clock_class
*bt_stream_class_borrow_default_clock_class(
436 struct bt_stream_class
*stream_class
)
438 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
439 return stream_class
->default_clock_class
;
442 bt_bool
bt_stream_class_assigns_automatic_event_class_id(
443 struct bt_stream_class
*stream_class
)
445 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
446 return (bt_bool
) stream_class
->assigns_automatic_event_class_id
;
449 int bt_stream_class_set_assigns_automatic_event_class_id(
450 struct bt_stream_class
*stream_class
, bt_bool value
)
452 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
453 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
454 stream_class
->assigns_automatic_event_class_id
= (bool) value
;
455 BT_LIB_LOGV("Set stream class's automatic event class ID "
456 "assignment property: %!+S", stream_class
);
460 bt_bool
bt_stream_class_assigns_automatic_stream_id(
461 struct bt_stream_class
*stream_class
)
463 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
464 return (bt_bool
) stream_class
->assigns_automatic_stream_id
;
467 int bt_stream_class_set_assigns_automatic_stream_id(
468 struct bt_stream_class
*stream_class
, bt_bool value
)
470 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
471 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
472 stream_class
->assigns_automatic_stream_id
= (bool) value
;
473 BT_LIB_LOGV("Set stream class's automatic stream ID "
474 "assignment property: %!+S", stream_class
);
478 bt_bool
bt_stream_class_packets_have_discarded_event_counter_snapshot(
479 struct bt_stream_class
*stream_class
)
481 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
482 return (bt_bool
) stream_class
->packets_have_discarded_event_counter_snapshot
;
485 int bt_stream_class_set_packets_have_discarded_event_counter_snapshot(
486 struct bt_stream_class
*stream_class
, bt_bool value
)
488 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
489 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
490 stream_class
->packets_have_discarded_event_counter_snapshot
=
492 BT_LIB_LOGV("Set stream class's "
493 "\"packets have discarded event counter snapshot\" property: "
494 "%!+S", stream_class
);
498 bt_bool
bt_stream_class_packets_have_packet_counter_snapshot(
499 struct bt_stream_class
*stream_class
)
501 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
502 return (bt_bool
) stream_class
->packets_have_packet_counter_snapshot
;
505 int bt_stream_class_set_packets_have_packet_counter_snapshot(
506 struct bt_stream_class
*stream_class
, bt_bool value
)
508 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
509 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
510 stream_class
->packets_have_packet_counter_snapshot
=
512 BT_LIB_LOGV("Set stream class's "
513 "\"packets have packet counter snapshot\" property: "
514 "%!+S", stream_class
);
518 bt_bool
bt_stream_class_packets_have_default_beginning_clock_value(
519 struct bt_stream_class
*stream_class
)
521 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
522 return (bt_bool
) stream_class
->packets_have_default_beginning_cv
;
525 int bt_stream_class_set_packets_have_default_beginning_clock_value(
526 struct bt_stream_class
*stream_class
, bt_bool value
)
528 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
529 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
530 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
531 "Stream class does not have a default clock class: %!+S",
533 stream_class
->packets_have_default_beginning_cv
= (bool) value
;
534 BT_LIB_LOGV("Set stream class's "
535 "\"packets have default beginning clock value\" property: "
536 "%!+S", stream_class
);
540 bt_bool
bt_stream_class_packets_have_default_end_clock_value(
541 struct bt_stream_class
*stream_class
)
543 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
544 return (bt_bool
) stream_class
->packets_have_default_end_cv
;
547 int bt_stream_class_set_packets_have_default_end_clock_value(
548 struct bt_stream_class
*stream_class
, bt_bool value
)
550 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
551 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
552 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
553 "Stream class does not have a default clock class: %!+S",
555 stream_class
->packets_have_default_end_cv
= (bool) value
;
556 BT_LIB_LOGV("Set stream class's "
557 "\"packets have default end clock value\" property: "
558 "%!+S", stream_class
);
562 bt_bool
bt_stream_class_default_clock_is_always_known(
563 struct bt_stream_class
*stream_class
)
565 /* BT_CLOCK_VALUE_STATUS_UNKNOWN is not supported as of 2.0 */