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 "lib/lib-logging.h"
27 #include "lib/assert-pre.h"
28 #include <babeltrace2/trace-ir/trace-const.h>
29 #include "compat/compiler.h"
30 #include "common/align.h"
31 #include "compat/endian.h"
32 #include "common/assert.h"
33 #include "lib/property.h"
38 #include "clock-class.h"
39 #include "event-class.h"
40 #include "field-class.h"
42 #include "field-wrapper.h"
43 #include "resolve-field-path.h"
44 #include "stream-class.h"
48 #define BT_ASSERT_PRE_STREAM_CLASS_HOT(_sc) \
49 BT_ASSERT_PRE_HOT((_sc), "Stream class", ": %!+S", (_sc))
52 void destroy_stream_class(struct bt_object
*obj
)
54 struct bt_stream_class
*stream_class
= (void *) obj
;
56 BT_LIB_LOGD("Destroying stream class: %!+S", stream_class
);
57 BT_LOGD_STR("Putting default clock class.");
58 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->default_clock_class
);
60 if (stream_class
->event_classes
) {
61 BT_LOGD_STR("Destroying event classes.");
62 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
63 stream_class
->event_classes
= NULL
;
66 if (stream_class
->name
.str
) {
67 g_string_free(stream_class
->name
.str
, TRUE
);
68 stream_class
->name
.str
= NULL
;
69 stream_class
->name
.value
= NULL
;
72 BT_LOGD_STR("Putting packet context field class.");
73 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->packet_context_fc
);
74 BT_LOGD_STR("Putting event common context field class.");
75 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->event_common_context_fc
);
76 bt_object_pool_finalize(&stream_class
->packet_context_field_pool
);
81 void free_field_wrapper(struct bt_field_wrapper
*field_wrapper
,
82 struct bt_stream_class
*stream_class
)
84 bt_field_wrapper_destroy((void *) field_wrapper
);
89 bool stream_class_id_is_unique(const struct bt_trace_class
*tc
, uint64_t id
)
92 bool is_unique
= true;
94 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
95 const struct bt_stream_class
*sc
=
96 tc
->stream_classes
->pdata
[i
];
109 struct bt_stream_class
*create_stream_class_with_id(
110 struct bt_trace_class
*tc
, uint64_t id
)
112 struct bt_stream_class
*stream_class
= NULL
;
116 BT_ASSERT_PRE(stream_class_id_is_unique(tc
, id
),
117 "Duplicate stream class ID: %![tc-]+T, id=%" PRIu64
, tc
, id
);
118 BT_LIB_LOGD("Creating stream class object: %![tc-]+T, id=%" PRIu64
,
120 stream_class
= g_new0(struct bt_stream_class
, 1);
122 BT_LOGE_STR("Failed to allocate one stream class.");
126 bt_object_init_shared_with_parent(&stream_class
->base
,
127 destroy_stream_class
);
129 stream_class
->name
.str
= g_string_new(NULL
);
130 if (!stream_class
->name
.str
) {
131 BT_LOGE_STR("Failed to allocate a GString.");
136 stream_class
->id
= id
;
137 stream_class
->assigns_automatic_event_class_id
= true;
138 stream_class
->assigns_automatic_stream_id
= true;
139 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
140 (GDestroyNotify
) bt_object_try_spec_release
);
141 if (!stream_class
->event_classes
) {
142 BT_LOGE_STR("Failed to allocate a GPtrArray.");
146 ret
= bt_object_pool_initialize(&stream_class
->packet_context_field_pool
,
147 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
148 (bt_object_pool_destroy_object_func
) free_field_wrapper
,
151 BT_LOGE("Failed to initialize packet context field pool: ret=%d",
156 bt_object_set_parent(&stream_class
->base
, &tc
->base
);
157 g_ptr_array_add(tc
->stream_classes
, stream_class
);
158 bt_trace_class_freeze(tc
);
159 BT_LIB_LOGD("Created stream class object: %!+S", stream_class
);
163 BT_OBJECT_PUT_REF_AND_RESET(stream_class
);
169 struct bt_stream_class
*bt_stream_class_create(struct bt_trace_class
*tc
)
171 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
172 BT_ASSERT_PRE(tc
->assigns_automatic_stream_class_id
,
173 "Trace class does not automatically assigns stream class IDs: "
175 return create_stream_class_with_id(tc
,
176 (uint64_t) tc
->stream_classes
->len
);
179 struct bt_stream_class
*bt_stream_class_create_with_id(
180 struct bt_trace_class
*tc
, uint64_t id
)
182 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
183 BT_ASSERT_PRE(!tc
->assigns_automatic_stream_class_id
,
184 "Trace class automatically assigns stream class IDs: "
186 return create_stream_class_with_id(tc
, id
);
189 struct bt_trace_class
*bt_stream_class_borrow_trace_class(
190 struct bt_stream_class
*stream_class
)
192 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
193 return bt_stream_class_borrow_trace_class_inline(stream_class
);
196 const struct bt_trace_class
*bt_stream_class_borrow_trace_class_const(
197 const struct bt_stream_class
*stream_class
)
199 return bt_stream_class_borrow_trace_class((void *) stream_class
);
202 const char *bt_stream_class_get_name(const struct bt_stream_class
*stream_class
)
204 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
205 return stream_class
->name
.value
;
208 enum bt_stream_class_status
bt_stream_class_set_name(
209 struct bt_stream_class
*stream_class
,
212 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
213 BT_ASSERT_PRE_NON_NULL(name
, "Name");
214 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
215 g_string_assign(stream_class
->name
.str
, name
);
216 stream_class
->name
.value
= stream_class
->name
.str
->str
;
217 BT_LIB_LOGD("Set stream class's name: %!+S", stream_class
);
218 return BT_STREAM_CLASS_STATUS_OK
;
221 uint64_t bt_stream_class_get_id(const struct bt_stream_class
*stream_class
)
223 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
224 return stream_class
->id
;
227 uint64_t bt_stream_class_get_event_class_count(
228 const struct bt_stream_class
*stream_class
)
230 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
231 return (uint64_t) stream_class
->event_classes
->len
;
234 struct bt_event_class
*bt_stream_class_borrow_event_class_by_index(
235 struct bt_stream_class
*stream_class
, uint64_t index
)
237 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
238 BT_ASSERT_PRE_VALID_INDEX(index
, stream_class
->event_classes
->len
);
239 return g_ptr_array_index(stream_class
->event_classes
, index
);
242 const struct bt_event_class
*
243 bt_stream_class_borrow_event_class_by_index_const(
244 const struct bt_stream_class
*stream_class
, uint64_t index
)
246 return bt_stream_class_borrow_event_class_by_index(
247 (void *) stream_class
, index
);
250 struct bt_event_class
*bt_stream_class_borrow_event_class_by_id(
251 struct bt_stream_class
*stream_class
, uint64_t id
)
253 struct bt_event_class
*event_class
= NULL
;
256 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
258 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
259 struct bt_event_class
*event_class_candidate
=
260 g_ptr_array_index(stream_class
->event_classes
, i
);
262 if (event_class_candidate
->id
== id
) {
263 event_class
= event_class_candidate
;
272 const struct bt_event_class
*
273 bt_stream_class_borrow_event_class_by_id_const(
274 const struct bt_stream_class
*stream_class
, uint64_t id
)
276 return bt_stream_class_borrow_event_class_by_id(
277 (void *) stream_class
, id
);
280 const struct bt_field_class
*
281 bt_stream_class_borrow_packet_context_field_class_const(
282 const struct bt_stream_class
*stream_class
)
284 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
285 return stream_class
->packet_context_fc
;
288 struct bt_field_class
*
289 bt_stream_class_borrow_packet_context_field_class(
290 struct bt_stream_class
*stream_class
)
292 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
293 return stream_class
->packet_context_fc
;
296 enum bt_stream_class_status
bt_stream_class_set_packet_context_field_class(
297 struct bt_stream_class
*stream_class
,
298 struct bt_field_class
*field_class
)
301 struct bt_resolve_field_path_context resolve_ctx
= {
302 .packet_context
= field_class
,
303 .event_common_context
= NULL
,
304 .event_specific_context
= NULL
,
305 .event_payload
= NULL
,
308 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
309 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
310 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
311 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
312 BT_FIELD_CLASS_TYPE_STRUCTURE
,
313 "Packet context field class is not a structure field class: %!+F",
315 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
318 * This is the only reason for which
319 * bt_resolve_field_paths() can fail: anything else
320 * would be because a precondition is not satisfied.
322 ret
= BT_STREAM_CLASS_STATUS_NOMEM
;
326 bt_field_class_make_part_of_trace_class(field_class
);
327 bt_object_put_ref(stream_class
->packet_context_fc
);
328 stream_class
->packet_context_fc
= field_class
;
329 bt_object_get_no_null_check(stream_class
->packet_context_fc
);
330 bt_field_class_freeze(field_class
);
331 BT_LIB_LOGD("Set stream class's packet context field class: %!+S",
338 const struct bt_field_class
*
339 bt_stream_class_borrow_event_common_context_field_class_const(
340 const struct bt_stream_class
*stream_class
)
342 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
343 return stream_class
->event_common_context_fc
;
346 struct bt_field_class
*
347 bt_stream_class_borrow_event_common_context_field_class(
348 struct bt_stream_class
*stream_class
)
350 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
351 return stream_class
->event_common_context_fc
;
354 enum bt_stream_class_status
355 bt_stream_class_set_event_common_context_field_class(
356 struct bt_stream_class
*stream_class
,
357 struct bt_field_class
*field_class
)
360 struct bt_resolve_field_path_context resolve_ctx
= {
361 .packet_context
= NULL
,
362 .event_common_context
= field_class
,
363 .event_specific_context
= NULL
,
364 .event_payload
= NULL
,
367 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
368 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
369 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
370 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
371 BT_FIELD_CLASS_TYPE_STRUCTURE
,
372 "Event common context field class is not a structure field class: %!+F",
374 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
375 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
378 * This is the only reason for which
379 * bt_resolve_field_paths() can fail: anything else
380 * would be because a precondition is not satisfied.
382 ret
= BT_STREAM_CLASS_STATUS_NOMEM
;
386 bt_field_class_make_part_of_trace_class(field_class
);
387 bt_object_put_ref(stream_class
->event_common_context_fc
);
388 stream_class
->event_common_context_fc
= field_class
;
389 bt_object_get_no_null_check(stream_class
->event_common_context_fc
);
390 bt_field_class_freeze(field_class
);
391 BT_LIB_LOGD("Set stream class's event common context field class: %!+S",
399 void _bt_stream_class_freeze(const struct bt_stream_class
*stream_class
)
401 /* The field classes and default clock class are already frozen */
402 BT_ASSERT(stream_class
);
403 BT_LIB_LOGD("Freezing stream class: %!+S", stream_class
);
404 ((struct bt_stream_class
*) stream_class
)->frozen
= true;
407 enum bt_stream_class_status
bt_stream_class_set_default_clock_class(
408 struct bt_stream_class
*stream_class
,
409 struct bt_clock_class
*clock_class
)
411 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
412 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
413 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
414 bt_object_put_ref(stream_class
->default_clock_class
);
415 stream_class
->default_clock_class
= clock_class
;
416 bt_object_get_no_null_check(stream_class
->default_clock_class
);
417 bt_clock_class_freeze(clock_class
);
418 BT_LIB_LOGD("Set stream class's default clock class: %!+S",
420 return BT_STREAM_CLASS_STATUS_OK
;
423 struct bt_clock_class
*bt_stream_class_borrow_default_clock_class(
424 struct bt_stream_class
*stream_class
)
426 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
427 return stream_class
->default_clock_class
;
430 const struct bt_clock_class
*bt_stream_class_borrow_default_clock_class_const(
431 const struct bt_stream_class
*stream_class
)
433 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
434 return stream_class
->default_clock_class
;
437 bt_bool
bt_stream_class_assigns_automatic_event_class_id(
438 const struct bt_stream_class
*stream_class
)
440 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
441 return (bt_bool
) stream_class
->assigns_automatic_event_class_id
;
444 void bt_stream_class_set_assigns_automatic_event_class_id(
445 struct bt_stream_class
*stream_class
,
448 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
449 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
450 stream_class
->assigns_automatic_event_class_id
= (bool) value
;
451 BT_LIB_LOGD("Set stream class's automatic event class ID "
452 "assignment property: %!+S", stream_class
);
455 bt_bool
bt_stream_class_packets_have_beginning_default_clock_snapshot(
456 const struct bt_stream_class
*stream_class
)
458 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
459 return (bt_bool
) stream_class
->packets_have_beginning_default_clock_snapshot
;
462 void bt_stream_class_set_packets_have_beginning_default_clock_snapshot(
463 struct bt_stream_class
*stream_class
, bt_bool value
)
465 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
466 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
467 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
468 "Stream class has no default clock class: %!+S", stream_class
);
469 stream_class
->packets_have_beginning_default_clock_snapshot
=
471 BT_LIB_LOGD("Set stream class's \"packets have default beginning "
472 "clock snapshot\" property: %!+S", stream_class
);
475 bt_bool
bt_stream_class_packets_have_end_default_clock_snapshot(
476 const struct bt_stream_class
*stream_class
)
478 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
479 return (bt_bool
) stream_class
->packets_have_end_default_clock_snapshot
;
482 void bt_stream_class_set_packets_have_end_default_clock_snapshot(
483 struct bt_stream_class
*stream_class
, bt_bool value
)
485 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
486 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
487 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
488 "Stream class has no default clock class: %!+S", stream_class
);
489 stream_class
->packets_have_end_default_clock_snapshot
=
491 BT_LIB_LOGD("Set stream class's \"packets have default end "
492 "clock snapshot\" property: %!+S", stream_class
);
495 bt_bool
bt_stream_class_assigns_automatic_stream_id(
496 const struct bt_stream_class
*stream_class
)
498 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
499 return (bt_bool
) stream_class
->assigns_automatic_stream_id
;
502 void bt_stream_class_set_supports_discarded_events(
503 struct bt_stream_class
*stream_class
,
504 bt_bool supports_discarded_events
,
505 bt_bool with_default_clock_snapshots
)
507 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
508 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
509 BT_ASSERT_PRE(supports_discarded_events
||
510 !with_default_clock_snapshots
,
511 "Discarded events cannot have default clock snapshots when "
512 "not supported: %!+S", stream_class
);
513 BT_ASSERT_PRE(!with_default_clock_snapshots
||
514 stream_class
->default_clock_class
,
515 "Stream class has no default clock class: %!+S", stream_class
);
516 stream_class
->supports_discarded_events
=
517 (bool) supports_discarded_events
;
518 stream_class
->discarded_events_have_default_clock_snapshots
=
519 (bool) with_default_clock_snapshots
;
520 BT_LIB_LOGD("Set stream class's discarded events support property: "
521 "%!+S", stream_class
);
524 bt_bool
bt_stream_class_supports_discarded_events(
525 const struct bt_stream_class
*stream_class
)
527 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
528 return (bt_bool
) stream_class
->supports_discarded_events
;
531 bt_bool
bt_stream_class_discarded_events_have_default_clock_snapshots(
532 const struct bt_stream_class
*stream_class
)
534 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
535 return (bt_bool
) stream_class
->discarded_events_have_default_clock_snapshots
;
538 void bt_stream_class_set_supports_discarded_packets(
539 struct bt_stream_class
*stream_class
,
540 bt_bool supports_discarded_packets
,
541 bt_bool with_default_clock_snapshots
)
543 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
544 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
545 BT_ASSERT_PRE(supports_discarded_packets
||
546 !with_default_clock_snapshots
,
547 "Discarded packets cannot have default clock snapshots when "
548 "not supported: %!+S", stream_class
);
549 BT_ASSERT_PRE(!with_default_clock_snapshots
||
550 stream_class
->default_clock_class
,
551 "Stream class has no default clock class: %!+S", stream_class
);
552 stream_class
->supports_discarded_packets
=
553 (bool) supports_discarded_packets
;
554 stream_class
->discarded_packets_have_default_clock_snapshots
=
555 (bool) with_default_clock_snapshots
;
556 BT_LIB_LOGD("Set stream class's discarded packets support property: "
557 "%!+S", stream_class
);
560 bt_bool
bt_stream_class_supports_discarded_packets(
561 const struct bt_stream_class
*stream_class
)
563 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
564 return (bt_bool
) stream_class
->supports_discarded_packets
;
567 bt_bool
bt_stream_class_discarded_packets_have_default_clock_snapshots(
568 const struct bt_stream_class
*stream_class
)
570 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
571 return (bt_bool
) stream_class
->discarded_packets_have_default_clock_snapshots
;
574 void bt_stream_class_set_assigns_automatic_stream_id(
575 struct bt_stream_class
*stream_class
,
578 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
579 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
580 stream_class
->assigns_automatic_stream_id
= (bool) value
;
581 BT_LIB_LOGD("Set stream class's automatic stream ID "
582 "assignment property: %!+S", stream_class
);
585 void bt_stream_class_get_ref(const struct bt_stream_class
*stream_class
)
587 bt_object_get_ref(stream_class
);
590 void bt_stream_class_put_ref(const struct bt_stream_class
*stream_class
)
592 bt_object_put_ref(stream_class
);