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/utils.h>
39 #include <babeltrace/ctf-ir/utils-internal.h>
40 #include <babeltrace/ctf-ir/field-wrapper-internal.h>
41 #include <babeltrace/ctf-ir/resolve-field-path-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>
47 #include <babeltrace/property-internal.h>
52 #define BT_ASSERT_PRE_STREAM_CLASS_HOT(_sc) \
53 BT_ASSERT_PRE_HOT((_sc), "Stream class", ": %!+S", (_sc))
56 void destroy_stream_class(struct bt_object
*obj
)
58 struct bt_stream_class
*stream_class
= (void *) obj
;
60 BT_LIB_LOGD("Destroying stream class: %!+S", stream_class
);
61 BT_LOGD_STR("Putting default clock class.");
62 bt_put(stream_class
->default_clock_class
);
64 if (stream_class
->event_classes
) {
65 BT_LOGD_STR("Destroying event classes.");
66 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
69 if (stream_class
->name
.str
) {
70 g_string_free(stream_class
->name
.str
, TRUE
);
73 BT_LOGD_STR("Putting event header field type.");
74 bt_put(stream_class
->event_header_ft
);
75 BT_LOGD_STR("Putting packet context field type.");
76 bt_put(stream_class
->packet_context_ft
);
77 BT_LOGD_STR("Putting event common context field type.");
78 bt_put(stream_class
->event_common_context_ft
);
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(struct bt_trace
*trace
, uint64_t id
)
96 bool is_unique
= true;
98 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
99 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_PUT(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(struct bt_stream_class
*stream_class
)
206 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
207 return bt_stream_class_borrow_trace_inline(stream_class
);
210 const char *bt_stream_class_get_name(struct bt_stream_class
*stream_class
)
212 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
213 return stream_class
->name
.value
;
216 int bt_stream_class_set_name(struct bt_stream_class
*stream_class
,
219 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
220 BT_ASSERT_PRE_NON_NULL(name
, "Name");
221 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
222 g_string_assign(stream_class
->name
.str
, name
);
223 stream_class
->name
.value
= stream_class
->name
.str
->str
;
224 BT_LIB_LOGV("Set stream class's name: %!+S", stream_class
);
228 uint64_t bt_stream_class_get_id(struct bt_stream_class
*stream_class
)
230 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
231 return stream_class
->id
;
234 uint64_t bt_stream_class_get_event_class_count(
235 struct bt_stream_class
*stream_class
)
237 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
238 return (uint64_t) stream_class
->event_classes
->len
;
241 struct bt_event_class
*bt_stream_class_borrow_event_class_by_index(
242 struct bt_stream_class
*stream_class
, uint64_t index
)
244 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
245 BT_ASSERT_PRE_VALID_INDEX(index
, stream_class
->event_classes
->len
);
246 return g_ptr_array_index(stream_class
->event_classes
, index
);
249 struct bt_event_class
*bt_stream_class_borrow_event_class_by_id(
250 struct bt_stream_class
*trace
, uint64_t id
)
252 struct bt_event_class
*event_class
= NULL
;
255 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
257 for (i
= 0; i
< trace
->event_classes
->len
; i
++) {
258 struct bt_event_class
*event_class_candidate
=
259 g_ptr_array_index(trace
->event_classes
, i
);
261 if (event_class_candidate
->id
== id
) {
262 event_class
= event_class_candidate
;
271 struct bt_field_type
*bt_stream_class_borrow_packet_context_field_type(
272 struct bt_stream_class
*stream_class
)
274 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
275 return stream_class
->packet_context_ft
;
278 int bt_stream_class_set_packet_context_field_type(
279 struct bt_stream_class
*stream_class
,
280 struct bt_field_type
*field_type
)
283 struct bt_resolve_field_path_context resolve_ctx
= {
284 .packet_header
= NULL
,
285 .packet_context
= field_type
,
286 .event_header
= NULL
,
287 .event_common_context
= NULL
,
288 .event_specific_context
= NULL
,
289 .event_payload
= NULL
,
292 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
293 BT_ASSERT_PRE_NON_NULL(field_type
, "Field type");
294 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
295 BT_ASSERT_PRE(bt_field_type_get_type_id(field_type
) ==
296 BT_FIELD_TYPE_ID_STRUCTURE
,
297 "Packet context field type is not a structure field type: %!+F",
299 resolve_ctx
.packet_header
=
300 bt_stream_class_borrow_trace_inline(stream_class
)->packet_header_ft
;
301 ret
= bt_resolve_field_paths(field_type
, &resolve_ctx
);
306 bt_field_type_make_part_of_trace(field_type
);
307 bt_put(stream_class
->packet_context_ft
);
308 stream_class
->packet_context_ft
= bt_get(field_type
);
309 bt_field_type_freeze(field_type
);
310 BT_LIB_LOGV("Set stream class's packet context field type: %!+S",
317 struct bt_field_type
*bt_stream_class_borrow_event_header_field_type(
318 struct bt_stream_class
*stream_class
)
320 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
321 return stream_class
->event_header_ft
;
324 int bt_stream_class_set_event_header_field_type(
325 struct bt_stream_class
*stream_class
,
326 struct bt_field_type
*field_type
)
329 struct bt_resolve_field_path_context resolve_ctx
= {
330 .packet_header
= NULL
,
331 .packet_context
= NULL
,
332 .event_header
= field_type
,
333 .event_common_context
= NULL
,
334 .event_specific_context
= NULL
,
335 .event_payload
= NULL
,
338 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
339 BT_ASSERT_PRE_NON_NULL(field_type
, "Field type");
340 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
341 BT_ASSERT_PRE(bt_field_type_get_type_id(field_type
) ==
342 BT_FIELD_TYPE_ID_STRUCTURE
,
343 "Event header field type is not a structure field type: %!+F",
345 resolve_ctx
.packet_header
=
346 bt_stream_class_borrow_trace_inline(stream_class
)->packet_header_ft
;
347 resolve_ctx
.packet_context
= stream_class
->packet_context_ft
;
348 ret
= bt_resolve_field_paths(field_type
, &resolve_ctx
);
353 bt_field_type_make_part_of_trace(field_type
);
354 bt_put(stream_class
->event_header_ft
);
355 stream_class
->event_header_ft
= bt_get(field_type
);
356 bt_field_type_freeze(field_type
);
357 BT_LIB_LOGV("Set stream class's event header field type: %!+S",
364 struct bt_field_type
*bt_stream_class_borrow_event_common_context_field_type(
365 struct bt_stream_class
*stream_class
)
367 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
368 return stream_class
->event_common_context_ft
;
371 int bt_stream_class_set_event_common_context_field_type(
372 struct bt_stream_class
*stream_class
,
373 struct bt_field_type
*field_type
)
376 struct bt_resolve_field_path_context resolve_ctx
= {
377 .packet_header
= NULL
,
378 .packet_context
= NULL
,
379 .event_header
= NULL
,
380 .event_common_context
= field_type
,
381 .event_specific_context
= NULL
,
382 .event_payload
= NULL
,
385 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
386 BT_ASSERT_PRE_NON_NULL(field_type
, "Field type");
387 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
388 BT_ASSERT_PRE(bt_field_type_get_type_id(field_type
) ==
389 BT_FIELD_TYPE_ID_STRUCTURE
,
390 "Event common context field type is not a structure field type: %!+F",
392 resolve_ctx
.packet_header
=
393 bt_stream_class_borrow_trace_inline(stream_class
)->packet_header_ft
;
394 resolve_ctx
.packet_context
= stream_class
->packet_context_ft
;
395 resolve_ctx
.event_header
= stream_class
->event_header_ft
;
396 ret
= bt_resolve_field_paths(field_type
, &resolve_ctx
);
401 bt_field_type_make_part_of_trace(field_type
);
402 bt_put(stream_class
->event_common_context_ft
);
403 stream_class
->event_common_context_ft
= bt_get(field_type
);
404 bt_field_type_freeze(field_type
);
405 BT_LIB_LOGV("Set stream class's event common context field type: %!+S",
413 void _bt_stream_class_freeze(struct bt_stream_class
*stream_class
)
415 /* The field types and default clock class are already frozen */
416 BT_ASSERT(stream_class
);
417 BT_LIB_LOGD("Freezing stream class: %!+S", stream_class
);
418 stream_class
->frozen
= true;
421 int bt_stream_class_set_default_clock_class(
422 struct bt_stream_class
*stream_class
,
423 struct bt_clock_class
*clock_class
)
425 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
426 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
427 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
428 bt_put(stream_class
->default_clock_class
);
429 stream_class
->default_clock_class
= bt_get(clock_class
);
430 bt_clock_class_freeze(clock_class
);
431 BT_LIB_LOGV("Set stream class's default clock class: %!+S",
436 struct bt_clock_class
*bt_stream_class_borrow_default_clock_class(
437 struct bt_stream_class
*stream_class
)
439 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
440 return stream_class
->default_clock_class
;
443 bt_bool
bt_stream_class_assigns_automatic_event_class_id(
444 struct bt_stream_class
*stream_class
)
446 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
447 return (bt_bool
) stream_class
->assigns_automatic_event_class_id
;
450 int bt_stream_class_set_assigns_automatic_event_class_id(
451 struct bt_stream_class
*stream_class
, bt_bool value
)
453 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
454 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
455 stream_class
->assigns_automatic_event_class_id
= (bool) value
;
456 BT_LIB_LOGV("Set stream class's automatic event class ID "
457 "assignment property: %!+S", stream_class
);
461 bt_bool
bt_stream_class_assigns_automatic_stream_id(
462 struct bt_stream_class
*stream_class
)
464 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
465 return (bt_bool
) stream_class
->assigns_automatic_stream_id
;
468 int bt_stream_class_set_assigns_automatic_stream_id(
469 struct bt_stream_class
*stream_class
, bt_bool value
)
471 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
472 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
473 stream_class
->assigns_automatic_stream_id
= (bool) value
;
474 BT_LIB_LOGV("Set stream class's automatic stream ID "
475 "assignment property: %!+S", stream_class
);
479 bt_bool
bt_stream_class_packets_have_discarded_event_counter_snapshot(
480 struct bt_stream_class
*stream_class
)
482 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
483 return (bt_bool
) stream_class
->packets_have_discarded_event_counter_snapshot
;
486 int bt_stream_class_set_packets_have_discarded_event_counter_snapshot(
487 struct bt_stream_class
*stream_class
, bt_bool value
)
489 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
490 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
491 stream_class
->packets_have_discarded_event_counter_snapshot
=
493 BT_LIB_LOGV("Set stream class's "
494 "\"packets have discarded event counter snapshot\" property: "
495 "%!+S", stream_class
);
499 bt_bool
bt_stream_class_packets_have_packet_counter_snapshot(
500 struct bt_stream_class
*stream_class
)
502 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
503 return (bt_bool
) stream_class
->packets_have_packet_counter_snapshot
;
506 int bt_stream_class_set_packets_have_packet_counter_snapshot(
507 struct bt_stream_class
*stream_class
, bt_bool value
)
509 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
510 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
511 stream_class
->packets_have_packet_counter_snapshot
=
513 BT_LIB_LOGV("Set stream class's "
514 "\"packets have packet counter snapshot\" property: "
515 "%!+S", stream_class
);
519 bt_bool
bt_stream_class_packets_have_default_beginning_clock_value(
520 struct bt_stream_class
*stream_class
)
522 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
523 return (bt_bool
) stream_class
->packets_have_default_beginning_cv
;
526 int bt_stream_class_set_packets_have_default_beginning_clock_value(
527 struct bt_stream_class
*stream_class
, bt_bool value
)
529 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
530 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
531 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
532 "Stream class does not have a default clock class: %!+S",
534 stream_class
->packets_have_default_beginning_cv
= (bool) value
;
535 BT_LIB_LOGV("Set stream class's "
536 "\"packets have default beginning clock value\" property: "
537 "%!+S", stream_class
);
541 bt_bool
bt_stream_class_packets_have_default_end_clock_value(
542 struct bt_stream_class
*stream_class
)
544 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
545 return (bt_bool
) stream_class
->packets_have_default_end_cv
;
548 int bt_stream_class_set_packets_have_default_end_clock_value(
549 struct bt_stream_class
*stream_class
, bt_bool value
)
551 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
552 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
553 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
554 "Stream class does not have a default clock class: %!+S",
556 stream_class
->packets_have_default_end_cv
= (bool) value
;
557 BT_LIB_LOGV("Set stream class's "
558 "\"packets have default end clock value\" property: "
559 "%!+S", stream_class
);
563 bt_bool
bt_stream_class_default_clock_is_always_known(
564 struct bt_stream_class
*stream_class
)
566 /* BT_CLOCK_VALUE_STATUS_UNKNOWN is not supported as of 2.0 */