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 "LIB/STREAM-CLASS"
25 #include "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"
47 #include "lib/func-status.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 packet context field class.");
74 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->packet_context_fc
);
75 BT_LOGD_STR("Putting event common context field class.");
76 BT_OBJECT_PUT_REF_AND_RESET(stream_class
->event_common_context_fc
);
77 bt_object_pool_finalize(&stream_class
->packet_context_field_pool
);
82 void free_field_wrapper(struct bt_field_wrapper
*field_wrapper
,
83 struct bt_stream_class
*stream_class
)
85 bt_field_wrapper_destroy((void *) field_wrapper
);
90 bool stream_class_id_is_unique(const struct bt_trace_class
*tc
, uint64_t id
)
93 bool is_unique
= true;
95 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
96 const struct bt_stream_class
*sc
=
97 tc
->stream_classes
->pdata
[i
];
110 struct bt_stream_class
*create_stream_class_with_id(
111 struct bt_trace_class
*tc
, uint64_t id
)
113 struct bt_stream_class
*stream_class
= NULL
;
117 BT_ASSERT_PRE(stream_class_id_is_unique(tc
, id
),
118 "Duplicate stream class ID: %![tc-]+T, id=%" PRIu64
, tc
, id
);
119 BT_LIB_LOGD("Creating stream class object: %![tc-]+T, id=%" PRIu64
,
121 stream_class
= g_new0(struct bt_stream_class
, 1);
123 BT_LOGE_STR("Failed to allocate one stream class.");
127 bt_object_init_shared_with_parent(&stream_class
->base
,
128 destroy_stream_class
);
130 stream_class
->name
.str
= g_string_new(NULL
);
131 if (!stream_class
->name
.str
) {
132 BT_LOGE_STR("Failed to allocate a GString.");
137 stream_class
->id
= id
;
138 stream_class
->assigns_automatic_event_class_id
= true;
139 stream_class
->assigns_automatic_stream_id
= true;
140 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
141 (GDestroyNotify
) bt_object_try_spec_release
);
142 if (!stream_class
->event_classes
) {
143 BT_LOGE_STR("Failed to allocate a GPtrArray.");
147 ret
= bt_object_pool_initialize(&stream_class
->packet_context_field_pool
,
148 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
149 (bt_object_pool_destroy_object_func
) free_field_wrapper
,
152 BT_LOGE("Failed to initialize packet context field pool: ret=%d",
157 bt_object_set_parent(&stream_class
->base
, &tc
->base
);
158 g_ptr_array_add(tc
->stream_classes
, stream_class
);
159 bt_trace_class_freeze(tc
);
160 BT_LIB_LOGD("Created stream class object: %!+S", stream_class
);
164 BT_OBJECT_PUT_REF_AND_RESET(stream_class
);
170 struct bt_stream_class
*bt_stream_class_create(struct bt_trace_class
*tc
)
172 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
173 BT_ASSERT_PRE(tc
->assigns_automatic_stream_class_id
,
174 "Trace class does not automatically assigns stream class IDs: "
176 return create_stream_class_with_id(tc
,
177 (uint64_t) tc
->stream_classes
->len
);
180 struct bt_stream_class
*bt_stream_class_create_with_id(
181 struct bt_trace_class
*tc
, uint64_t id
)
183 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
184 BT_ASSERT_PRE(!tc
->assigns_automatic_stream_class_id
,
185 "Trace class automatically assigns stream class IDs: "
187 return create_stream_class_with_id(tc
, id
);
190 struct bt_trace_class
*bt_stream_class_borrow_trace_class(
191 struct bt_stream_class
*stream_class
)
193 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
194 return bt_stream_class_borrow_trace_class_inline(stream_class
);
197 const struct bt_trace_class
*bt_stream_class_borrow_trace_class_const(
198 const struct bt_stream_class
*stream_class
)
200 return bt_stream_class_borrow_trace_class((void *) stream_class
);
203 const char *bt_stream_class_get_name(const struct bt_stream_class
*stream_class
)
205 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
206 return stream_class
->name
.value
;
209 enum bt_stream_class_set_name_status
bt_stream_class_set_name(
210 struct bt_stream_class
*stream_class
,
213 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
214 BT_ASSERT_PRE_NON_NULL(name
, "Name");
215 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
216 g_string_assign(stream_class
->name
.str
, name
);
217 stream_class
->name
.value
= stream_class
->name
.str
->str
;
218 BT_LIB_LOGD("Set stream class's name: %!+S", stream_class
);
219 return BT_FUNC_STATUS_OK
;
222 uint64_t bt_stream_class_get_id(const struct bt_stream_class
*stream_class
)
224 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
225 return stream_class
->id
;
228 uint64_t bt_stream_class_get_event_class_count(
229 const struct bt_stream_class
*stream_class
)
231 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
232 return (uint64_t) stream_class
->event_classes
->len
;
235 struct bt_event_class
*bt_stream_class_borrow_event_class_by_index(
236 struct bt_stream_class
*stream_class
, uint64_t index
)
238 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
239 BT_ASSERT_PRE_VALID_INDEX(index
, stream_class
->event_classes
->len
);
240 return g_ptr_array_index(stream_class
->event_classes
, index
);
243 const struct bt_event_class
*
244 bt_stream_class_borrow_event_class_by_index_const(
245 const struct bt_stream_class
*stream_class
, uint64_t index
)
247 return bt_stream_class_borrow_event_class_by_index(
248 (void *) stream_class
, index
);
251 struct bt_event_class
*bt_stream_class_borrow_event_class_by_id(
252 struct bt_stream_class
*stream_class
, uint64_t id
)
254 struct bt_event_class
*event_class
= NULL
;
257 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
259 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
260 struct bt_event_class
*event_class_candidate
=
261 g_ptr_array_index(stream_class
->event_classes
, i
);
263 if (event_class_candidate
->id
== id
) {
264 event_class
= event_class_candidate
;
273 const struct bt_event_class
*
274 bt_stream_class_borrow_event_class_by_id_const(
275 const struct bt_stream_class
*stream_class
, uint64_t id
)
277 return bt_stream_class_borrow_event_class_by_id(
278 (void *) stream_class
, id
);
281 const struct bt_field_class
*
282 bt_stream_class_borrow_packet_context_field_class_const(
283 const struct bt_stream_class
*stream_class
)
285 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
286 return stream_class
->packet_context_fc
;
289 struct bt_field_class
*
290 bt_stream_class_borrow_packet_context_field_class(
291 struct bt_stream_class
*stream_class
)
293 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
294 return stream_class
->packet_context_fc
;
297 enum bt_stream_class_set_field_class_status
298 bt_stream_class_set_packet_context_field_class(
299 struct bt_stream_class
*stream_class
,
300 struct bt_field_class
*field_class
)
303 struct bt_resolve_field_path_context resolve_ctx
= {
304 .packet_context
= field_class
,
305 .event_common_context
= NULL
,
306 .event_specific_context
= NULL
,
307 .event_payload
= NULL
,
310 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
311 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
312 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
313 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
314 BT_FIELD_CLASS_TYPE_STRUCTURE
,
315 "Packet context field class is not a structure field class: %!+F",
317 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
320 * This is the only reason for which
321 * bt_resolve_field_paths() can fail: anything else
322 * would be because a precondition is not satisfied.
324 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
328 bt_field_class_make_part_of_trace_class(field_class
);
329 bt_object_put_ref(stream_class
->packet_context_fc
);
330 stream_class
->packet_context_fc
= field_class
;
331 bt_object_get_no_null_check(stream_class
->packet_context_fc
);
332 bt_field_class_freeze(field_class
);
333 BT_LIB_LOGD("Set stream class's packet context field class: %!+S",
340 const struct bt_field_class
*
341 bt_stream_class_borrow_event_common_context_field_class_const(
342 const struct bt_stream_class
*stream_class
)
344 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
345 return stream_class
->event_common_context_fc
;
348 struct bt_field_class
*
349 bt_stream_class_borrow_event_common_context_field_class(
350 struct bt_stream_class
*stream_class
)
352 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
353 return stream_class
->event_common_context_fc
;
356 enum bt_stream_class_set_field_class_status
357 bt_stream_class_set_event_common_context_field_class(
358 struct bt_stream_class
*stream_class
,
359 struct bt_field_class
*field_class
)
362 struct bt_resolve_field_path_context resolve_ctx
= {
363 .packet_context
= NULL
,
364 .event_common_context
= field_class
,
365 .event_specific_context
= NULL
,
366 .event_payload
= NULL
,
369 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
370 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
371 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
372 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
373 BT_FIELD_CLASS_TYPE_STRUCTURE
,
374 "Event common context field class is not a structure field class: %!+F",
376 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
377 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
380 * This is the only reason for which
381 * bt_resolve_field_paths() can fail: anything else
382 * would be because a precondition is not satisfied.
384 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
388 bt_field_class_make_part_of_trace_class(field_class
);
389 bt_object_put_ref(stream_class
->event_common_context_fc
);
390 stream_class
->event_common_context_fc
= field_class
;
391 bt_object_get_no_null_check(stream_class
->event_common_context_fc
);
392 bt_field_class_freeze(field_class
);
393 BT_LIB_LOGD("Set stream class's event common context field class: %!+S",
401 void _bt_stream_class_freeze(const struct bt_stream_class
*stream_class
)
403 /* The field classes and default clock class are already frozen */
404 BT_ASSERT(stream_class
);
405 BT_LIB_LOGD("Freezing stream class: %!+S", stream_class
);
406 ((struct bt_stream_class
*) stream_class
)->frozen
= true;
409 enum bt_stream_class_set_default_clock_class_status
410 bt_stream_class_set_default_clock_class(
411 struct bt_stream_class
*stream_class
,
412 struct bt_clock_class
*clock_class
)
414 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
415 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
416 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
417 bt_object_put_ref(stream_class
->default_clock_class
);
418 stream_class
->default_clock_class
= clock_class
;
419 bt_object_get_no_null_check(stream_class
->default_clock_class
);
420 bt_clock_class_freeze(clock_class
);
421 BT_LIB_LOGD("Set stream class's default clock class: %!+S",
423 return BT_FUNC_STATUS_OK
;
426 struct bt_clock_class
*bt_stream_class_borrow_default_clock_class(
427 struct bt_stream_class
*stream_class
)
429 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
430 return stream_class
->default_clock_class
;
433 const struct bt_clock_class
*bt_stream_class_borrow_default_clock_class_const(
434 const struct bt_stream_class
*stream_class
)
436 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
437 return stream_class
->default_clock_class
;
440 bt_bool
bt_stream_class_assigns_automatic_event_class_id(
441 const struct bt_stream_class
*stream_class
)
443 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
444 return (bt_bool
) stream_class
->assigns_automatic_event_class_id
;
447 void bt_stream_class_set_assigns_automatic_event_class_id(
448 struct bt_stream_class
*stream_class
,
451 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
452 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
453 stream_class
->assigns_automatic_event_class_id
= (bool) value
;
454 BT_LIB_LOGD("Set stream class's automatic event class ID "
455 "assignment property: %!+S", stream_class
);
458 bt_bool
bt_stream_class_packets_have_beginning_default_clock_snapshot(
459 const struct bt_stream_class
*stream_class
)
461 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
462 return (bt_bool
) stream_class
->packets_have_beginning_default_clock_snapshot
;
465 void bt_stream_class_set_packets_have_beginning_default_clock_snapshot(
466 struct bt_stream_class
*stream_class
, bt_bool value
)
468 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
469 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
470 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
471 "Stream class has no default clock class: %!+S", stream_class
);
472 stream_class
->packets_have_beginning_default_clock_snapshot
=
474 BT_LIB_LOGD("Set stream class's \"packets have default beginning "
475 "clock snapshot\" property: %!+S", stream_class
);
478 bt_bool
bt_stream_class_packets_have_end_default_clock_snapshot(
479 const struct bt_stream_class
*stream_class
)
481 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
482 return (bt_bool
) stream_class
->packets_have_end_default_clock_snapshot
;
485 void bt_stream_class_set_packets_have_end_default_clock_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 BT_ASSERT_PRE(!value
|| stream_class
->default_clock_class
,
491 "Stream class has no default clock class: %!+S", stream_class
);
492 stream_class
->packets_have_end_default_clock_snapshot
=
494 BT_LIB_LOGD("Set stream class's \"packets have default end "
495 "clock snapshot\" 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_supports_discarded_events(
506 struct bt_stream_class
*stream_class
,
507 bt_bool supports_discarded_events
,
508 bt_bool with_default_clock_snapshots
)
510 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
511 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
512 BT_ASSERT_PRE(supports_discarded_events
||
513 !with_default_clock_snapshots
,
514 "Discarded events cannot have default clock snapshots when "
515 "not supported: %!+S", stream_class
);
516 BT_ASSERT_PRE(!with_default_clock_snapshots
||
517 stream_class
->default_clock_class
,
518 "Stream class has no default clock class: %!+S", stream_class
);
519 stream_class
->supports_discarded_events
=
520 (bool) supports_discarded_events
;
521 stream_class
->discarded_events_have_default_clock_snapshots
=
522 (bool) with_default_clock_snapshots
;
523 BT_LIB_LOGD("Set stream class's discarded events support property: "
524 "%!+S", stream_class
);
527 bt_bool
bt_stream_class_supports_discarded_events(
528 const struct bt_stream_class
*stream_class
)
530 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
531 return (bt_bool
) stream_class
->supports_discarded_events
;
534 bt_bool
bt_stream_class_discarded_events_have_default_clock_snapshots(
535 const struct bt_stream_class
*stream_class
)
537 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
538 return (bt_bool
) stream_class
->discarded_events_have_default_clock_snapshots
;
541 void bt_stream_class_set_supports_discarded_packets(
542 struct bt_stream_class
*stream_class
,
543 bt_bool supports_discarded_packets
,
544 bt_bool with_default_clock_snapshots
)
546 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
547 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
548 BT_ASSERT_PRE(supports_discarded_packets
||
549 !with_default_clock_snapshots
,
550 "Discarded packets cannot have default clock snapshots when "
551 "not supported: %!+S", stream_class
);
552 BT_ASSERT_PRE(!with_default_clock_snapshots
||
553 stream_class
->default_clock_class
,
554 "Stream class has no default clock class: %!+S", stream_class
);
555 stream_class
->supports_discarded_packets
=
556 (bool) supports_discarded_packets
;
557 stream_class
->discarded_packets_have_default_clock_snapshots
=
558 (bool) with_default_clock_snapshots
;
559 BT_LIB_LOGD("Set stream class's discarded packets support property: "
560 "%!+S", stream_class
);
563 bt_bool
bt_stream_class_supports_discarded_packets(
564 const struct bt_stream_class
*stream_class
)
566 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
567 return (bt_bool
) stream_class
->supports_discarded_packets
;
570 bt_bool
bt_stream_class_discarded_packets_have_default_clock_snapshots(
571 const struct bt_stream_class
*stream_class
)
573 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
574 return (bt_bool
) stream_class
->discarded_packets_have_default_clock_snapshots
;
577 void bt_stream_class_set_assigns_automatic_stream_id(
578 struct bt_stream_class
*stream_class
,
581 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
582 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class
);
583 stream_class
->assigns_automatic_stream_id
= (bool) value
;
584 BT_LIB_LOGD("Set stream class's automatic stream ID "
585 "assignment property: %!+S", stream_class
);
588 void bt_stream_class_get_ref(const struct bt_stream_class
*stream_class
)
590 bt_object_get_ref(stream_class
);
593 void bt_stream_class_put_ref(const struct bt_stream_class
*stream_class
)
595 bt_object_put_ref(stream_class
);