1 #ifndef BABELTRACE_CTF_IR_STREAM_CLASS_INTERNAL_H
2 #define BABELTRACE_CTF_IR_STREAM_CLASS_INTERNAL_H
5 * BabelTrace - CTF IR: Stream class internal
7 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <babeltrace/assert-internal.h>
31 #include <babeltrace/common-internal.h>
32 #include <babeltrace/ctf-ir/validation-internal.h>
33 #include <babeltrace/ctf-ir/field-types-internal.h>
34 #include <babeltrace/ctf-ir/utils-internal.h>
35 #include <babeltrace/ctf-ir/visitor.h>
36 #include <babeltrace/object-internal.h>
37 #include <babeltrace/babeltrace-internal.h>
41 struct bt_stream_class_common
{
42 struct bt_object base
;
45 /* Array of pointers to event class addresses */
46 GPtrArray
*event_classes
;
48 /* event class id (int64_t) to event class address */
49 GHashTable
*event_classes_ht
;
52 int64_t next_event_id
;
53 struct bt_field_type_common
*packet_context_field_type
;
54 struct bt_field_type_common
*event_header_field_type
;
55 struct bt_field_type_common
*event_context_field_type
;
60 * This flag indicates if the stream class is valid. A valid
61 * stream class is _always_ frozen.
66 * Unique clock class mapped to any field type within this
67 * stream class, including all the stream class's event class
68 * field types. This is only set if the stream class is frozen.
70 * If the stream class is frozen and this is still NULL, it is
71 * still possible that it becomes non-NULL because
72 * bt_stream_class_add_event_class() can add an event class
73 * containing a field type mapped to some clock class. In this
74 * case, this is the mapped clock class, and at this point, both
75 * the new event class and the stream class are frozen, so the
76 * next added event classes are expected to contain field types
77 * which only map to this specific clock class.
79 * If this is a CTF writer stream class, then this is the
80 * backing clock class of the `clock` member above.
82 struct bt_clock_class
*clock_class
;
85 struct bt_stream_class
{
86 struct bt_stream_class_common common
;
89 struct bt_event_class_common
;
92 int bt_stream_class_common_initialize(struct bt_stream_class_common
*stream_class
,
93 const char *name
, bt_object_release_func release_func
);
96 void bt_stream_class_common_finalize(struct bt_stream_class_common
*stream_class
);
99 void bt_stream_class_common_freeze(struct bt_stream_class_common
*stream_class
);
102 void bt_stream_class_freeze(struct bt_stream_class
*stream_class
);
105 const char *bt_stream_class_common_get_name(
106 struct bt_stream_class_common
*stream_class
)
108 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
109 return stream_class
->name
->len
> 0 ? stream_class
->name
->str
: NULL
;
113 int64_t bt_stream_class_common_get_id(
114 struct bt_stream_class_common
*stream_class
)
118 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
120 if (!stream_class
->id_set
) {
121 BT_LOGV("Stream class's ID is not set: addr=%p, name=\"%s\"",
123 bt_stream_class_common_get_name(stream_class
));
128 ret
= stream_class
->id
;
135 void bt_stream_class_common_set_byte_order(
136 struct bt_stream_class_common
*stream_class
, int byte_order
);
139 int bt_stream_class_common_validate_single_clock_class(
140 struct bt_stream_class_common
*stream_class
,
141 struct bt_clock_class
**expected_clock_class
);
144 int bt_stream_class_common_add_event_class(
145 struct bt_stream_class_common
*stream_class
,
146 struct bt_event_class_common
*event_class
,
147 bt_validation_flag_copy_field_type_func copy_field_type_func
);
150 int bt_stream_class_common_visit(struct bt_stream_class_common
*stream_class
,
151 bt_visitor visitor
, void *data
);
154 struct bt_trace_common
*bt_stream_class_common_borrow_trace(
155 struct bt_stream_class_common
*stream_class
)
157 BT_ASSERT(stream_class
);
158 return (void *) bt_object_borrow_parent(stream_class
);
162 int bt_stream_class_common_set_name(struct bt_stream_class_common
*stream_class
,
168 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
173 if (stream_class
->frozen
) {
174 BT_LOGW("Invalid parameter: stream class is frozen: "
175 "addr=%p, name=\"%s\", id=%" PRId64
,
177 bt_stream_class_common_get_name(stream_class
),
178 bt_stream_class_common_get_id(stream_class
));
184 g_string_assign(stream_class
->name
, "");
186 if (strlen(name
) == 0) {
187 BT_LOGW("Invalid parameter: name is empty.");
192 g_string_assign(stream_class
->name
, name
);
195 BT_LOGV("Set stream class's name: "
196 "addr=%p, name=\"%s\", id=%" PRId64
,
197 stream_class
, bt_stream_class_common_get_name(stream_class
),
198 bt_stream_class_common_get_id(stream_class
));
204 void _bt_stream_class_common_set_id(
205 struct bt_stream_class_common
*stream_class
, int64_t id
)
207 BT_ASSERT(stream_class
);
208 stream_class
->id
= id
;
209 stream_class
->id_set
= 1;
210 BT_LOGV("Set stream class's ID (internal): "
211 "addr=%p, name=\"%s\", id=%" PRId64
,
212 stream_class
, bt_stream_class_common_get_name(stream_class
),
213 bt_stream_class_common_get_id(stream_class
));
217 int bt_stream_class_common_set_id_no_check(
218 struct bt_stream_class_common
*stream_class
, int64_t id
)
220 _bt_stream_class_common_set_id(stream_class
, id
);
225 int bt_stream_class_common_set_id(struct bt_stream_class_common
*stream_class
,
229 int64_t id
= (int64_t) id_param
;
232 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
237 if (stream_class
->frozen
) {
238 BT_LOGW("Invalid parameter: stream class is frozen: "
239 "addr=%p, name=\"%s\", id=%" PRId64
,
241 bt_stream_class_common_get_name(stream_class
),
242 bt_stream_class_common_get_id(stream_class
));
248 BT_LOGW("Invalid parameter: invalid stream class's ID: "
249 "stream-class-addr=%p, stream-class-name=\"%s\", "
250 "stream-class-id=%" PRId64
", id=%" PRIu64
,
252 bt_stream_class_common_get_name(stream_class
),
253 bt_stream_class_common_get_id(stream_class
),
259 ret
= bt_stream_class_common_set_id_no_check(stream_class
, id
);
261 BT_LOGV("Set stream class's ID: "
262 "addr=%p, name=\"%s\", id=%" PRId64
,
264 bt_stream_class_common_get_name(stream_class
),
265 bt_stream_class_common_get_id(stream_class
));
272 int64_t bt_stream_class_common_get_event_class_count(
273 struct bt_stream_class_common
*stream_class
)
278 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
283 ret
= (int64_t) stream_class
->event_classes
->len
;
289 struct bt_event_class_common
*bt_stream_class_common_borrow_event_class_by_index(
290 struct bt_stream_class_common
*stream_class
, uint64_t index
)
292 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
293 BT_ASSERT_PRE(index
< stream_class
->event_classes
->len
,
294 "Index is out of bounds: index=%" PRIu64
", "
296 index
, stream_class
->event_classes
->len
);
297 return g_ptr_array_index(stream_class
->event_classes
, index
);
301 struct bt_event_class_common
*bt_stream_class_common_borrow_event_class_by_id(
302 struct bt_stream_class_common
*stream_class
, uint64_t id
)
304 int64_t id_key
= (int64_t) id
;
306 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
307 BT_ASSERT_PRE(id_key
>= 0,
308 "Invalid event class ID: %" PRIu64
, id
);
309 return g_hash_table_lookup(stream_class
->event_classes_ht
,
314 struct bt_field_type_common
*
315 bt_stream_class_common_borrow_packet_context_field_type(
316 struct bt_stream_class_common
*stream_class
)
318 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
319 return stream_class
->packet_context_field_type
;
323 int bt_stream_class_common_set_packet_context_field_type(
324 struct bt_stream_class_common
*stream_class
,
325 struct bt_field_type_common
*packet_context_type
)
330 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
335 if (stream_class
->frozen
) {
336 BT_LOGW("Invalid parameter: stream class is frozen: "
337 "addr=%p, name=\"%s\", id=%" PRId64
,
338 stream_class
, bt_stream_class_common_get_name(stream_class
),
339 bt_stream_class_common_get_id(stream_class
));
344 if (packet_context_type
&&
345 bt_field_type_common_get_type_id(packet_context_type
) !=
346 BT_FIELD_TYPE_ID_STRUCT
) {
347 /* A packet context must be a structure. */
348 BT_LOGW("Invalid parameter: stream class's packet context field type must be a structure: "
349 "addr=%p, name=\"%s\", id=%" PRId64
", "
350 "packet-context-ft-addr=%p, packet-context-ft-id=%s",
351 stream_class
, bt_stream_class_common_get_name(stream_class
),
352 bt_stream_class_common_get_id(stream_class
),
354 bt_common_field_type_id_string(
355 bt_field_type_common_get_type_id(packet_context_type
)));
360 bt_put(stream_class
->packet_context_field_type
);
361 bt_get(packet_context_type
);
362 stream_class
->packet_context_field_type
= packet_context_type
;
363 BT_LOGV("Set stream class's packet context field type: "
364 "addr=%p, name=\"%s\", id=%" PRId64
", "
365 "packet-context-ft-addr=%p",
366 stream_class
, bt_stream_class_common_get_name(stream_class
),
367 bt_stream_class_common_get_id(stream_class
),
368 packet_context_type
);
375 struct bt_field_type_common
*
376 bt_stream_class_common_borrow_event_header_field_type(
377 struct bt_stream_class_common
*stream_class
)
379 struct bt_field_type_common
*ret
= NULL
;
381 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
383 if (!stream_class
->event_header_field_type
) {
384 BT_LOGV("Stream class has no event header field type: "
385 "addr=%p, name=\"%s\", id=%" PRId64
,
387 bt_stream_class_common_get_name(stream_class
),
388 bt_stream_class_common_get_id(stream_class
));
392 ret
= stream_class
->event_header_field_type
;
399 int bt_stream_class_common_set_event_header_field_type(
400 struct bt_stream_class_common
*stream_class
,
401 struct bt_field_type_common
*event_header_type
)
406 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
411 if (stream_class
->frozen
) {
412 BT_LOGW("Invalid parameter: stream class is frozen: "
413 "addr=%p, name=\"%s\", id=%" PRId64
,
415 bt_stream_class_common_get_name(stream_class
),
416 bt_stream_class_common_get_id(stream_class
));
421 if (event_header_type
&&
422 bt_field_type_common_get_type_id(event_header_type
) !=
423 BT_FIELD_TYPE_ID_STRUCT
) {
424 /* An event header must be a structure. */
425 BT_LOGW("Invalid parameter: stream class's event header field type must be a structure: "
426 "addr=%p, name=\"%s\", id=%" PRId64
", "
427 "event-header-ft-addr=%p, event-header-ft-id=%s",
428 stream_class
, bt_stream_class_common_get_name(stream_class
),
429 bt_stream_class_common_get_id(stream_class
),
431 bt_common_field_type_id_string(
432 bt_field_type_common_get_type_id(event_header_type
)));
437 bt_put(stream_class
->event_header_field_type
);
438 stream_class
->event_header_field_type
= bt_get(event_header_type
);
439 BT_LOGV("Set stream class's event header field type: "
440 "addr=%p, name=\"%s\", id=%" PRId64
", "
441 "event-header-ft-addr=%p",
442 stream_class
, bt_stream_class_common_get_name(stream_class
),
443 bt_stream_class_common_get_id(stream_class
),
450 struct bt_field_type_common
*
451 bt_stream_class_common_borrow_event_context_field_type(
452 struct bt_stream_class_common
*stream_class
)
454 struct bt_field_type_common
*ret
= NULL
;
456 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
458 if (!stream_class
->event_context_field_type
) {
462 ret
= stream_class
->event_context_field_type
;
469 int bt_stream_class_common_set_event_context_field_type(
470 struct bt_stream_class_common
*stream_class
,
471 struct bt_field_type_common
*event_context_type
)
476 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
481 if (stream_class
->frozen
) {
482 BT_LOGW("Invalid parameter: stream class is frozen: "
483 "addr=%p, name=\"%s\", id=%" PRId64
,
484 stream_class
, bt_stream_class_common_get_name(stream_class
),
485 bt_stream_class_common_get_id(stream_class
));
490 if (event_context_type
&&
491 bt_field_type_common_get_type_id(event_context_type
) !=
492 BT_FIELD_TYPE_ID_STRUCT
) {
493 /* A packet context must be a structure. */
494 BT_LOGW("Invalid parameter: stream class's event context field type must be a structure: "
495 "addr=%p, name=\"%s\", id=%" PRId64
", "
496 "event-context-ft-addr=%p, event-context-ft-id=%s",
497 stream_class
, bt_stream_class_common_get_name(stream_class
),
498 bt_stream_class_common_get_id(stream_class
),
500 bt_common_field_type_id_string(
501 bt_field_type_common_get_type_id(event_context_type
)));
506 bt_put(stream_class
->event_context_field_type
);
507 stream_class
->event_context_field_type
= bt_get(event_context_type
);
508 BT_LOGV("Set stream class's event context field type: "
509 "addr=%p, name=\"%s\", id=%" PRId64
", "
510 "event-context-ft-addr=%p",
511 stream_class
, bt_stream_class_common_get_name(stream_class
),
512 bt_stream_class_common_get_id(stream_class
),
518 #endif /* BABELTRACE_CTF_IR_STREAM_CLASS_INTERNAL_H */