2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "STREAM-CLASS"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/assert-pre-internal.h>
28 #include <babeltrace/trace-ir/clock-class-internal.h>
29 #include <babeltrace/trace-ir/event-class-internal.h>
30 #include <babeltrace/trace-ir/field-class-internal.h>
31 #include <babeltrace/trace-ir/field-internal.h>
32 #include <babeltrace/trace-ir/stream-class-internal.h>
33 #include <babeltrace/trace-ir/trace-const.h>
34 #include <babeltrace/trace-ir/trace-internal.h>
35 #include <babeltrace/trace-ir/utils-internal.h>
36 #include <babeltrace/trace-ir/field-wrapper-internal.h>
37 #include <babeltrace/trace-ir/resolve-field-path-internal.h>
38 #include <babeltrace/compiler-internal.h>
39 #include <babeltrace/align-internal.h>
40 #include <babeltrace/endian-internal.h>
41 #include <babeltrace/assert-internal.h>
42 #include <babeltrace/property-internal.h>
47 #define BT_ASSERT_PRE_STREAM_CLASS_HOT(_sc) \
48 BT_ASSERT_PRE_HOT((_sc), "Stream class", ": %!+S", (_sc))
51 void destroy_stream_class(struct bt_object
*obj
)
53 struct bt_stream_class
*stream_class
= (void *) obj
;
55 BT_LIB_LOGD("Destroying stream class: %!+S", stream_class
);
56 BT_LOGD_STR("Putting default clock class.");
57 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->default_clock_class
);
59 if (stream_class
->event_classes
) {
60 BT_LOGD_STR("Destroying event classes.");
61 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
62 stream_class
->event_classes
= NULL
;
65 if (stream_class
->name
.str
) {
66 g_string_free(stream_class
->name
.str
, TRUE
);
67 stream_class
->name
.str
= NULL
;
68 stream_class
->name
.value
= NULL
;
71 BT_LOGD_STR("Putting packet context field class.");
72 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->packet_context_fc
);
73 BT_LOGD_STR("Putting event common context field class.");
74 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->event_common_context_fc
);
75 bt_object_pool_finalize(&stream_class
->packet_context_field_pool
);
80 void free_field_wrapper(struct bt_field_wrapper
*field_wrapper
,
81 struct bt_stream_class
*stream_class
)
83 bt_field_wrapper_destroy((void *) field_wrapper
);
88 bool stream_class_id_is_unique(const struct bt_trace_class
*tc
, uint64_t id
)
91 bool is_unique
= true;
93 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
94 const struct bt_stream_class
*sc
=
95 tc
->stream_classes
->pdata
[i
];
108 struct bt_stream_class
*create_stream_class_with_id(
109 struct bt_trace_class
*tc
, uint64_t id
)
111 struct bt_stream_class
*stream_class
= NULL
;
115 BT_ASSERT_PRE(stream_class_id_is_unique(tc
, id
),
116 "Duplicate stream class ID: %![tc-]+T, id=%" PRIu64
, tc
, id
);
117 BT_LIB_LOGD("Creating stream class object: %![tc-]+T, id=%" PRIu64
,
119 stream_class
= g_new0(struct bt_stream_class
, 1);
121 BT_LOGE_STR("Failed to allocate one stream class.");
125 bt_object_init_shared_with_parent(&stream_class
->base
,
126 destroy_stream_class
);
128 stream_class
->name
.str
= g_string_new(NULL
);
129 if (!stream_class
->name
.str
) {
130 BT_LOGE_STR("Failed to allocate a GString.");
135 stream_class
->id
= id
;
136 stream_class
->assigns_automatic_event_class_id
= true;
137 stream_class
->assigns_automatic_stream_id
= true;
138 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
139 (GDestroyNotify
) bt_object_try_spec_release
);
140 if (!stream_class
->event_classes
) {
141 BT_LOGE_STR("Failed to allocate a GPtrArray.");
145 ret
= bt_object_pool_initialize(&stream_class
->packet_context_field_pool
,
146 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
147 (bt_object_pool_destroy_object_func
) free_field_wrapper
,
150 BT_LOGE("Failed to initialize packet context field pool: ret=%d",
155 bt_object_set_parent(&stream_class
->base
, &tc
->base
);
156 g_ptr_array_add(tc
->stream_classes
, stream_class
);
157 bt_trace_class_freeze(tc
);
158 BT_LIB_LOGD("Created stream class object: %!+S", stream_class
);
162 BT_OBJECT_PUT_REF_AND_RESET(stream_class
);
168 struct bt_stream_class
*bt_stream_class_create(struct bt_trace_class
*tc
)
170 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
171 BT_ASSERT_PRE(tc
->assigns_automatic_stream_class_id
,
172 "Trace class does not automatically assigns stream class IDs: "
174 return create_stream_class_with_id(tc
,
175 (uint64_t) tc
->stream_classes
->len
);
178 struct bt_stream_class
*bt_stream_class_create_with_id(
179 struct bt_trace_class
*tc
, uint64_t id
)
181 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
182 BT_ASSERT_PRE(!tc
->assigns_automatic_stream_class_id
,
183 "Trace class automatically assigns stream class IDs: "
185 return create_stream_class_with_id(tc
, id
);
188 struct bt_trace_class
*bt_stream_class_borrow_trace_class(
189 struct bt_stream_class
*stream_class
)
191 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
192 return bt_stream_class_borrow_trace_class_inline(stream_class
);
195 const struct bt_trace_class
*bt_stream_class_borrow_trace_class_const(
196 const struct bt_stream_class
*stream_class
)
198 return bt_stream_class_borrow_trace_class((void *) stream_class
);
201 const char *bt_stream_class_get_name(const struct bt_stream_class
*stream_class
)
203 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
204 return stream_class
->name
.value
;
207 enum bt_stream_class_status
bt_stream_class_set_name(
208 struct bt_stream_class
*stream_class
,
211 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
212 BT_ASSERT_PRE_NON_NULL(name
, "Name");
213 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
214 g_string_assign(stream_class
->name
.str
, name
);
215 stream_class
->name
.value
= stream_class
->name
.str
->str
;
216 BT_LIB_LOGV("Set stream class's name: %!+S", stream_class
);
217 return BT_STREAM_CLASS_STATUS_OK
;
220 uint64_t bt_stream_class_get_id(const struct bt_stream_class
*stream_class
)
222 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
223 return stream_class
->id
;
226 uint64_t bt_stream_class_get_event_class_count(
227 const struct bt_stream_class
*stream_class
)
229 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
230 return (uint64_t) stream_class
->event_classes
->len
;
233 struct bt_event_class
*bt_stream_class_borrow_event_class_by_index(
234 struct bt_stream_class
*stream_class
, uint64_t index
)
236 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
237 BT_ASSERT_PRE_VALID_INDEX(index
, stream_class
->event_classes
->len
);
238 return g_ptr_array_index(stream_class
->event_classes
, index
);
241 const struct bt_event_class
*
242 bt_stream_class_borrow_event_class_by_index_const(
243 const struct bt_stream_class
*stream_class
, uint64_t index
)
245 return bt_stream_class_borrow_event_class_by_index(
246 (void *) stream_class
, index
);
249 struct bt_event_class
*bt_stream_class_borrow_event_class_by_id(
250 struct bt_stream_class
*stream_class
, uint64_t id
)
252 struct bt_event_class
*event_class
= NULL
;
255 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
257 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
258 struct bt_event_class
*event_class_candidate
=
259 g_ptr_array_index(stream_class
->event_classes
, i
);
261 if (event_class_candidate
->id
== id
) {
262 event_class
= event_class_candidate
;
271 const struct bt_event_class
*
272 bt_stream_class_borrow_event_class_by_id_const(
273 const struct bt_stream_class
*stream_class
, uint64_t id
)
275 return bt_stream_class_borrow_event_class_by_id(
276 (void *) stream_class
, id
);
279 const struct bt_field_class
*
280 bt_stream_class_borrow_packet_context_field_class_const(
281 const struct bt_stream_class
*stream_class
)
283 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
284 return stream_class
->packet_context_fc
;
287 struct bt_field_class
*
288 bt_stream_class_borrow_packet_context_field_class(
289 struct bt_stream_class
*stream_class
)
291 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
292 return stream_class
->packet_context_fc
;
295 enum bt_stream_class_status
bt_stream_class_set_packet_context_field_class(
296 struct bt_stream_class
*stream_class
,
297 struct bt_field_class
*field_class
)
300 struct bt_resolve_field_path_context resolve_ctx
= {
301 .packet_context
= field_class
,
302 .event_common_context
= NULL
,
303 .event_specific_context
= NULL
,
304 .event_payload
= NULL
,
307 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
308 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
309 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
310 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
311 BT_FIELD_CLASS_TYPE_STRUCTURE
,
312 "Packet context field class is not a structure field class: %!+F",
314 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
317 * This is the only reason for which
318 * bt_resolve_field_paths() can fail: anything else
319 * would be because a precondition is not satisfied.
321 ret
= BT_STREAM_CLASS_STATUS_NOMEM
;
325 bt_field_class_make_part_of_trace_class(field_class
);
326 bt_object_put_ref(stream_class
->packet_context_fc
);
327 stream_class
->packet_context_fc
= field_class
;
328 bt_object_get_no_null_check(stream_class
->packet_context_fc
);
329 bt_field_class_freeze(field_class
);
330 BT_LIB_LOGV("Set stream class's packet context field class: %!+S",
337 const struct bt_field_class
*
338 bt_stream_class_borrow_event_common_context_field_class_const(
339 const struct bt_stream_class
*stream_class
)
341 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
342 return stream_class
->event_common_context_fc
;
345 struct bt_field_class
*
346 bt_stream_class_borrow_event_common_context_field_class(
347 struct bt_stream_class
*stream_class
)
349 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
350 return stream_class
->event_common_context_fc
;
353 enum bt_stream_class_status
354 bt_stream_class_set_event_common_context_field_class(
355 struct bt_stream_class
*stream_class
,
356 struct bt_field_class
*field_class
)
359 struct bt_resolve_field_path_context resolve_ctx
= {
360 .packet_context
= NULL
,
361 .event_common_context
= field_class
,
362 .event_specific_context
= NULL
,
363 .event_payload
= NULL
,
366 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
367 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
368 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
369 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
370 BT_FIELD_CLASS_TYPE_STRUCTURE
,
371 "Event common context field class is not a structure field class: %!+F",
373 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
374 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
377 * This is the only reason for which
378 * bt_resolve_field_paths() can fail: anything else
379 * would be because a precondition is not satisfied.
381 ret
= BT_STREAM_CLASS_STATUS_NOMEM
;
385 bt_field_class_make_part_of_trace_class(field_class
);
386 bt_object_put_ref(stream_class
->event_common_context_fc
);
387 stream_class
->event_common_context_fc
= field_class
;
388 bt_object_get_no_null_check(stream_class
->event_common_context_fc
);
389 bt_field_class_freeze(field_class
);
390 BT_LIB_LOGV("Set stream class's event common context field class: %!+S",
398 void _bt_stream_class_freeze(const struct bt_stream_class
*stream_class
)
400 /* The field classes and default clock class are already frozen */
401 BT_ASSERT(stream_class
);
402 BT_LIB_LOGD("Freezing stream class: %!+S", stream_class
);
403 ((struct bt_stream_class
*) stream_class
)->frozen
= true;
406 enum bt_stream_class_status
bt_stream_class_set_default_clock_class(
407 struct bt_stream_class
*stream_class
,
408 struct bt_clock_class
*clock_class
)
410 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
411 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
412 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
413 bt_object_put_ref(stream_class
->default_clock_class
);
414 stream_class
->default_clock_class
= clock_class
;
415 bt_object_get_no_null_check(stream_class
->default_clock_class
);
416 bt_clock_class_freeze(clock_class
);
417 BT_LIB_LOGV("Set stream class's default clock class: %!+S",
419 return BT_STREAM_CLASS_STATUS_OK
;
422 struct bt_clock_class
*bt_stream_class_borrow_default_clock_class(
423 struct bt_stream_class
*stream_class
)
425 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
426 return stream_class
->default_clock_class
;
429 const struct bt_clock_class
*bt_stream_class_borrow_default_clock_class_const(
430 const struct bt_stream_class
*stream_class
)
432 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
433 return stream_class
->default_clock_class
;
436 bt_bool
bt_stream_class_assigns_automatic_event_class_id(
437 const struct bt_stream_class
*stream_class
)
439 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
440 return (bt_bool
) stream_class
->assigns_automatic_event_class_id
;
443 void bt_stream_class_set_assigns_automatic_event_class_id(
444 struct bt_stream_class
*stream_class
,
447 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
448 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
449 stream_class
->assigns_automatic_event_class_id
= (bool) value
;
450 BT_LIB_LOGV("Set stream class's automatic event class ID "
451 "assignment property: %!+S", stream_class
);
454 bt_bool
bt_stream_class_packets_have_beginning_default_clock_snapshot(
455 const struct bt_stream_class
*stream_class
)
457 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
458 return (bt_bool
) stream_class
->packets_have_beginning_default_clock_snapshot
;
461 void bt_stream_class_set_packets_have_beginning_default_clock_snapshot(
462 struct bt_stream_class
*stream_class
, bt_bool value
)
464 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
465 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
466 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
467 "Stream class has no default clock class: %!+S", stream_class
);
468 stream_class
->packets_have_beginning_default_clock_snapshot
=
470 BT_LIB_LOGV("Set stream class's \"packets have default beginning "
471 "clock snapshot\" property: %!+S", stream_class
);
474 bt_bool
bt_stream_class_packets_have_end_default_clock_snapshot(
475 const struct bt_stream_class
*stream_class
)
477 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
478 return (bt_bool
) stream_class
->packets_have_end_default_clock_snapshot
;
481 void bt_stream_class_set_packets_have_end_default_clock_snapshot(
482 struct bt_stream_class
*stream_class
, bt_bool value
)
484 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
485 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
486 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
487 "Stream class has no default clock class: %!+S", stream_class
);
488 stream_class
->packets_have_end_default_clock_snapshot
=
490 BT_LIB_LOGV("Set stream class's \"packets have default end "
491 "clock snapshot\" property: %!+S", stream_class
);
494 bt_bool
bt_stream_class_assigns_automatic_stream_id(
495 const struct bt_stream_class
*stream_class
)
497 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
498 return (bt_bool
) stream_class
->assigns_automatic_stream_id
;
501 void bt_stream_class_set_supports_discarded_events(
502 struct bt_stream_class
*stream_class
,
503 bt_bool supports_discarded_events
,
504 bt_bool with_default_clock_snapshots
)
506 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
507 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
508 BT_ASSERT_PRE(supports_discarded_events
||
509 !with_default_clock_snapshots
,
510 "Discarded events cannot have default clock snapshots when "
511 "not supported: %!+S", stream_class
);
512 BT_ASSERT_PRE(!with_default_clock_snapshots
||
513 stream_class
->default_clock_class
,
514 "Stream class has no default clock class: %!+S", stream_class
);
515 stream_class
->supports_discarded_events
=
516 (bool) supports_discarded_events
;
517 stream_class
->discarded_events_have_default_clock_snapshots
=
518 (bool) with_default_clock_snapshots
;
519 BT_LIB_LOGV("Set stream class's discarded events support property: "
520 "%!+S", stream_class
);
523 bt_bool
bt_stream_class_supports_discarded_events(
524 const struct bt_stream_class
*stream_class
)
526 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
527 return (bt_bool
) stream_class
->supports_discarded_events
;
530 bt_bool
bt_stream_class_discarded_events_have_default_clock_snapshots(
531 const struct bt_stream_class
*stream_class
)
533 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
534 return (bt_bool
) stream_class
->discarded_events_have_default_clock_snapshots
;
537 void bt_stream_class_set_supports_discarded_packets(
538 struct bt_stream_class
*stream_class
,
539 bt_bool supports_discarded_packets
,
540 bt_bool with_default_clock_snapshots
)
542 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
543 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
544 BT_ASSERT_PRE(supports_discarded_packets
||
545 !with_default_clock_snapshots
,
546 "Discarded packets cannot have default clock snapshots when "
547 "not supported: %!+S", stream_class
);
548 BT_ASSERT_PRE(!with_default_clock_snapshots
||
549 stream_class
->default_clock_class
,
550 "Stream class has no default clock class: %!+S", stream_class
);
551 stream_class
->supports_discarded_packets
=
552 (bool) supports_discarded_packets
;
553 stream_class
->discarded_packets_have_default_clock_snapshots
=
554 (bool) with_default_clock_snapshots
;
555 BT_LIB_LOGV("Set stream class's discarded packets support property: "
556 "%!+S", stream_class
);
559 bt_bool
bt_stream_class_supports_discarded_packets(
560 const struct bt_stream_class
*stream_class
)
562 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
563 return (bt_bool
) stream_class
->supports_discarded_packets
;
566 bt_bool
bt_stream_class_discarded_packets_have_default_clock_snapshots(
567 const struct bt_stream_class
*stream_class
)
569 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
570 return (bt_bool
) stream_class
->discarded_packets_have_default_clock_snapshots
;
573 void bt_stream_class_set_assigns_automatic_stream_id(
574 struct bt_stream_class
*stream_class
,
577 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
578 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
579 stream_class
->assigns_automatic_stream_id
= (bool) value
;
580 BT_LIB_LOGV("Set stream class's automatic stream ID "
581 "assignment property: %!+S", stream_class
);
584 void bt_stream_class_get_ref(const struct bt_stream_class
*stream_class
)
586 bt_object_get_ref(stream_class
);
589 void bt_stream_class_put_ref(const struct bt_stream_class
*stream_class
)
591 bt_object_put_ref(stream_class
);