1 #ifndef BABELTRACE_CTF_WRITER_STREAM_CLASS_INTERNAL_H
2 #define BABELTRACE_CTF_WRITER_STREAM_CLASS_INTERNAL_H
5 * BabelTrace - CTF Writer: Stream Class
7 * Copyright 2014 EfficiOS Inc.
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
29 * The Common Trace Format (CTF) Specification is available at
30 * http://www.efficios.com/ctf
33 #include <babeltrace/assert-internal.h>
34 #include <babeltrace/babeltrace-internal.h>
35 #include <babeltrace/common-internal.h>
36 #include <babeltrace/ctf-writer/clock-internal.h>
37 #include <babeltrace/ctf-writer/field-types-internal.h>
38 #include <babeltrace/ctf-writer/field-types.h>
39 #include <babeltrace/ctf-writer/stream-class-internal.h>
40 #include <babeltrace/ctf-writer/utils-internal.h>
41 #include <babeltrace/ctf-writer/validation-internal.h>
42 #include <babeltrace/ctf-writer/visitor.h>
43 #include <babeltrace/object-internal.h>
46 struct bt_ctf_stream_class_common
{
47 struct bt_object base
;
50 /* Array of pointers to event class addresses */
51 GPtrArray
*event_classes
;
53 /* event class id (int64_t) to event class address */
54 GHashTable
*event_classes_ht
;
57 int64_t next_event_id
;
58 struct bt_ctf_field_type_common
*packet_context_field_type
;
59 struct bt_ctf_field_type_common
*event_header_field_type
;
60 struct bt_ctf_field_type_common
*event_context_field_type
;
65 * This flag indicates if the stream class is valid. A valid
66 * stream class is _always_ frozen.
71 * Unique clock class mapped to any field type within this
72 * stream class, including all the stream class's event class
73 * field types. This is only set if the stream class is frozen.
75 * If the stream class is frozen and this is still NULL, it is
76 * still possible that it becomes non-NULL because
77 * bt_ctf_stream_class_add_event_class() can add an event class
78 * containing a field type mapped to some clock class. In this
79 * case, this is the mapped clock class, and at this point, both
80 * the new event class and the stream class are frozen, so the
81 * next added event classes are expected to contain field types
82 * which only map to this specific clock class.
84 * If this is a CTF writer stream class, then this is the
85 * backing clock class of the `clock` member above.
87 struct bt_ctf_clock_class
*clock_class
;
90 struct bt_ctf_event_class_common
;
93 int bt_ctf_stream_class_common_initialize(struct bt_ctf_stream_class_common
*stream_class
,
94 const char *name
, bt_object_release_func release_func
);
97 void bt_ctf_stream_class_common_finalize(struct bt_ctf_stream_class_common
*stream_class
);
100 void bt_ctf_stream_class_common_freeze(struct bt_ctf_stream_class_common
*stream_class
);
103 const char *bt_ctf_stream_class_common_get_name(
104 struct bt_ctf_stream_class_common
*stream_class
)
106 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
107 return stream_class
->name
->len
> 0 ? stream_class
->name
->str
: NULL
;
111 int64_t bt_ctf_stream_class_common_get_id(
112 struct bt_ctf_stream_class_common
*stream_class
)
116 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
118 if (!stream_class
->id_set
) {
119 BT_LOGV("Stream class's ID is not set: addr=%p, name=\"%s\"",
121 bt_ctf_stream_class_common_get_name(stream_class
));
126 ret
= stream_class
->id
;
133 void bt_ctf_stream_class_common_set_byte_order(
134 struct bt_ctf_stream_class_common
*stream_class
, int byte_order
);
137 int bt_ctf_stream_class_common_validate_single_clock_class(
138 struct bt_ctf_stream_class_common
*stream_class
,
139 struct bt_ctf_clock_class
**expected_clock_class
);
142 int bt_ctf_stream_class_common_add_event_class(
143 struct bt_ctf_stream_class_common
*stream_class
,
144 struct bt_ctf_event_class_common
*event_class
,
145 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
);
148 int bt_ctf_stream_class_common_visit(struct bt_ctf_stream_class_common
*stream_class
,
149 bt_ctf_visitor visitor
, void *data
);
152 int bt_ctf_stream_class_visit(struct bt_ctf_stream_class
*stream_class
,
153 bt_ctf_visitor visitor
, void *data
);
156 struct bt_ctf_trace_common
*bt_ctf_stream_class_common_borrow_trace(
157 struct bt_ctf_stream_class_common
*stream_class
)
159 BT_ASSERT(stream_class
);
160 return (void *) bt_object_borrow_parent(&stream_class
->base
);
164 int bt_ctf_stream_class_common_set_name(struct bt_ctf_stream_class_common
*stream_class
,
170 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
175 if (stream_class
->frozen
) {
176 BT_LOGW("Invalid parameter: stream class is frozen: "
177 "addr=%p, name=\"%s\", id=%" PRId64
,
179 bt_ctf_stream_class_common_get_name(stream_class
),
180 bt_ctf_stream_class_common_get_id(stream_class
));
186 g_string_assign(stream_class
->name
, "");
188 if (strlen(name
) == 0) {
189 BT_LOGW("Invalid parameter: name is empty.");
194 g_string_assign(stream_class
->name
, name
);
197 BT_LOGV("Set stream class's name: "
198 "addr=%p, name=\"%s\", id=%" PRId64
,
199 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
200 bt_ctf_stream_class_common_get_id(stream_class
));
206 void _bt_ctf_stream_class_common_set_id(
207 struct bt_ctf_stream_class_common
*stream_class
, int64_t id
)
209 BT_ASSERT(stream_class
);
210 stream_class
->id
= id
;
211 stream_class
->id_set
= 1;
212 BT_LOGV("Set stream class's ID (internal): "
213 "addr=%p, name=\"%s\", id=%" PRId64
,
214 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
215 bt_ctf_stream_class_common_get_id(stream_class
));
219 int bt_ctf_stream_class_common_set_id_no_check(
220 struct bt_ctf_stream_class_common
*stream_class
, int64_t id
)
222 _bt_ctf_stream_class_common_set_id(stream_class
, id
);
227 int bt_ctf_stream_class_common_set_id(struct bt_ctf_stream_class_common
*stream_class
,
231 int64_t id
= (int64_t) id_param
;
234 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
239 if (stream_class
->frozen
) {
240 BT_LOGW("Invalid parameter: stream class is frozen: "
241 "addr=%p, name=\"%s\", id=%" PRId64
,
243 bt_ctf_stream_class_common_get_name(stream_class
),
244 bt_ctf_stream_class_common_get_id(stream_class
));
250 BT_LOGW("Invalid parameter: invalid stream class's ID: "
251 "stream-class-addr=%p, stream-class-name=\"%s\", "
252 "stream-class-id=%" PRId64
", id=%" PRIu64
,
254 bt_ctf_stream_class_common_get_name(stream_class
),
255 bt_ctf_stream_class_common_get_id(stream_class
),
261 ret
= bt_ctf_stream_class_common_set_id_no_check(stream_class
, id
);
263 BT_LOGV("Set stream class's ID: "
264 "addr=%p, name=\"%s\", id=%" PRId64
,
266 bt_ctf_stream_class_common_get_name(stream_class
),
267 bt_ctf_stream_class_common_get_id(stream_class
));
274 int64_t bt_ctf_stream_class_common_get_event_class_count(
275 struct bt_ctf_stream_class_common
*stream_class
)
280 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
285 ret
= (int64_t) stream_class
->event_classes
->len
;
291 struct bt_ctf_event_class_common
*bt_ctf_stream_class_common_borrow_event_class_by_index(
292 struct bt_ctf_stream_class_common
*stream_class
, uint64_t index
)
294 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
295 BT_ASSERT_PRE(index
< stream_class
->event_classes
->len
,
296 "Index is out of bounds: index=%" PRIu64
", "
298 index
, stream_class
->event_classes
->len
);
299 return g_ptr_array_index(stream_class
->event_classes
, index
);
303 struct bt_ctf_event_class_common
*bt_ctf_stream_class_common_borrow_event_class_by_id(
304 struct bt_ctf_stream_class_common
*stream_class
, uint64_t id
)
306 int64_t id_key
= (int64_t) id
;
308 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
309 BT_ASSERT_PRE(id_key
>= 0,
310 "Invalid event class ID: %" PRIu64
, id
);
311 return g_hash_table_lookup(stream_class
->event_classes_ht
,
316 struct bt_ctf_field_type_common
*
317 bt_ctf_stream_class_common_borrow_packet_context_field_type(
318 struct bt_ctf_stream_class_common
*stream_class
)
320 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
321 return stream_class
->packet_context_field_type
;
325 int bt_ctf_stream_class_common_set_packet_context_field_type(
326 struct bt_ctf_stream_class_common
*stream_class
,
327 struct bt_ctf_field_type_common
*packet_context_type
)
332 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
337 if (stream_class
->frozen
) {
338 BT_LOGW("Invalid parameter: stream class is frozen: "
339 "addr=%p, name=\"%s\", id=%" PRId64
,
340 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
341 bt_ctf_stream_class_common_get_id(stream_class
));
346 if (packet_context_type
&&
347 bt_ctf_field_type_common_get_type_id(packet_context_type
) !=
348 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
349 /* A packet context must be a structure. */
350 BT_LOGW("Invalid parameter: stream class's packet context field type must be a structure: "
351 "addr=%p, name=\"%s\", id=%" PRId64
", "
352 "packet-context-ft-addr=%p, packet-context-ft-id=%s",
353 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
354 bt_ctf_stream_class_common_get_id(stream_class
),
356 bt_ctf_field_type_id_string(
357 bt_ctf_field_type_common_get_type_id(packet_context_type
)));
362 bt_object_put_ref(stream_class
->packet_context_field_type
);
363 bt_object_get_ref(packet_context_type
);
364 stream_class
->packet_context_field_type
= packet_context_type
;
365 BT_LOGV("Set stream class's packet context field type: "
366 "addr=%p, name=\"%s\", id=%" PRId64
", "
367 "packet-context-ft-addr=%p",
368 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
369 bt_ctf_stream_class_common_get_id(stream_class
),
370 packet_context_type
);
377 struct bt_ctf_field_type_common
*
378 bt_ctf_stream_class_common_borrow_event_header_field_type(
379 struct bt_ctf_stream_class_common
*stream_class
)
381 struct bt_ctf_field_type_common
*ret
= NULL
;
383 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
385 if (!stream_class
->event_header_field_type
) {
386 BT_LOGV("Stream class has no event header field type: "
387 "addr=%p, name=\"%s\", id=%" PRId64
,
389 bt_ctf_stream_class_common_get_name(stream_class
),
390 bt_ctf_stream_class_common_get_id(stream_class
));
394 ret
= stream_class
->event_header_field_type
;
401 int bt_ctf_stream_class_common_set_event_header_field_type(
402 struct bt_ctf_stream_class_common
*stream_class
,
403 struct bt_ctf_field_type_common
*event_header_type
)
408 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
413 if (stream_class
->frozen
) {
414 BT_LOGW("Invalid parameter: stream class is frozen: "
415 "addr=%p, name=\"%s\", id=%" PRId64
,
417 bt_ctf_stream_class_common_get_name(stream_class
),
418 bt_ctf_stream_class_common_get_id(stream_class
));
423 if (event_header_type
&&
424 bt_ctf_field_type_common_get_type_id(event_header_type
) !=
425 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
426 /* An event header must be a structure. */
427 BT_LOGW("Invalid parameter: stream class's event header field type must be a structure: "
428 "addr=%p, name=\"%s\", id=%" PRId64
", "
429 "event-header-ft-addr=%p, event-header-ft-id=%s",
430 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
431 bt_ctf_stream_class_common_get_id(stream_class
),
433 bt_ctf_field_type_id_string(
434 bt_ctf_field_type_common_get_type_id(event_header_type
)));
439 bt_object_put_ref(stream_class
->event_header_field_type
);
440 stream_class
->event_header_field_type
= bt_object_get_ref(event_header_type
);
441 BT_LOGV("Set stream class's event header field type: "
442 "addr=%p, name=\"%s\", id=%" PRId64
", "
443 "event-header-ft-addr=%p",
444 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
445 bt_ctf_stream_class_common_get_id(stream_class
),
452 struct bt_ctf_field_type_common
*
453 bt_ctf_stream_class_common_borrow_event_context_field_type(
454 struct bt_ctf_stream_class_common
*stream_class
)
456 struct bt_ctf_field_type_common
*ret
= NULL
;
458 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
460 if (!stream_class
->event_context_field_type
) {
464 ret
= stream_class
->event_context_field_type
;
471 int bt_ctf_stream_class_common_set_event_context_field_type(
472 struct bt_ctf_stream_class_common
*stream_class
,
473 struct bt_ctf_field_type_common
*event_context_type
)
478 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
483 if (stream_class
->frozen
) {
484 BT_LOGW("Invalid parameter: stream class is frozen: "
485 "addr=%p, name=\"%s\", id=%" PRId64
,
486 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
487 bt_ctf_stream_class_common_get_id(stream_class
));
492 if (event_context_type
&&
493 bt_ctf_field_type_common_get_type_id(event_context_type
) !=
494 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
495 /* A packet context must be a structure. */
496 BT_LOGW("Invalid parameter: stream class's event context field type must be a structure: "
497 "addr=%p, name=\"%s\", id=%" PRId64
", "
498 "event-context-ft-addr=%p, event-context-ft-id=%s",
499 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
500 bt_ctf_stream_class_common_get_id(stream_class
),
502 bt_ctf_field_type_id_string(
503 bt_ctf_field_type_common_get_type_id(event_context_type
)));
508 bt_object_put_ref(stream_class
->event_context_field_type
);
509 stream_class
->event_context_field_type
= bt_object_get_ref(event_context_type
);
510 BT_LOGV("Set stream class's event context field type: "
511 "addr=%p, name=\"%s\", id=%" PRId64
", "
512 "event-context-ft-addr=%p",
513 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
514 bt_ctf_stream_class_common_get_id(stream_class
),
520 struct bt_ctf_stream_class
{
521 struct bt_ctf_stream_class_common common
;
522 struct bt_ctf_clock
*clock
;
523 int64_t next_stream_id
;
526 struct metadata_context
;
529 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
530 struct metadata_context
*context
);
533 int bt_ctf_stream_class_map_clock_class(
534 struct bt_ctf_stream_class
*stream_class
,
535 struct bt_ctf_field_type
*packet_context_type
,
536 struct bt_ctf_field_type
*event_header_type
);
538 #endif /* BABELTRACE_CTF_WRITER_STREAM_CLASS_INTERNAL_H */