lib: fully detach CTF IR and CTF writer implementations
[babeltrace.git] / include / babeltrace / ctf-writer / stream-class-internal.h
CommitLineData
3dca2276
PP
1#ifndef BABELTRACE_CTF_WRITER_STREAM_CLASS_INTERNAL_H
2#define BABELTRACE_CTF_WRITER_STREAM_CLASS_INTERNAL_H
3
4/*
5 * BabelTrace - CTF Writer: Stream Class
6 *
7 * Copyright 2014 EfficiOS Inc.
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
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
27 * SOFTWARE.
28 *
29 * The Common Trace Format (CTF) Specification is available at
30 * http://www.efficios.com/ctf
31 */
32
16ca5ff0
PP
33#include <babeltrace/assert-internal.h>
34#include <babeltrace/babeltrace-internal.h>
35#include <babeltrace/common-internal.h>
3dca2276 36#include <babeltrace/ctf-writer/clock-internal.h>
16ca5ff0
PP
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>
44#include <inttypes.h>
45
46struct bt_ctf_stream_class_common {
47 struct bt_object base;
48 GString *name;
49
50 /* Array of pointers to event class addresses */
51 GPtrArray *event_classes;
52
53 /* event class id (int64_t) to event class address */
54 GHashTable *event_classes_ht;
55 int id_set;
56 int64_t id;
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;
61 int frozen;
62 int byte_order;
63
64 /*
65 * This flag indicates if the stream class is valid. A valid
66 * stream class is _always_ frozen.
67 */
68 int valid;
69
70 /*
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.
74 *
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.
83 *
84 * If this is a CTF writer stream class, then this is the
85 * backing clock class of the `clock` member above.
86 */
87 struct bt_ctf_clock_class *clock_class;
88};
89
90struct bt_ctf_event_class_common;
91
92BT_HIDDEN
93int bt_ctf_stream_class_common_initialize(struct bt_ctf_stream_class_common *stream_class,
94 const char *name, bt_object_release_func release_func);
95
96BT_HIDDEN
97void bt_ctf_stream_class_common_finalize(struct bt_ctf_stream_class_common *stream_class);
98
99BT_HIDDEN
100void bt_ctf_stream_class_common_freeze(struct bt_ctf_stream_class_common *stream_class);
101
102static inline
103const char *bt_ctf_stream_class_common_get_name(
104 struct bt_ctf_stream_class_common *stream_class)
105{
106 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
107 return stream_class->name->len > 0 ? stream_class->name->str : NULL;
108}
109
110static inline
111int64_t bt_ctf_stream_class_common_get_id(
112 struct bt_ctf_stream_class_common *stream_class)
113{
114 int64_t ret;
115
116 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
117
118 if (!stream_class->id_set) {
119 BT_LOGV("Stream class's ID is not set: addr=%p, name=\"%s\"",
120 stream_class,
121 bt_ctf_stream_class_common_get_name(stream_class));
122 ret = (int64_t) -1;
123 goto end;
124 }
125
126 ret = stream_class->id;
127
128end:
129 return ret;
130}
131
132BT_HIDDEN
133void bt_ctf_stream_class_common_set_byte_order(
134 struct bt_ctf_stream_class_common *stream_class, int byte_order);
135
136BT_HIDDEN
137int 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);
140
141BT_HIDDEN
142int 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);
146
147BT_HIDDEN
148int bt_ctf_stream_class_common_visit(struct bt_ctf_stream_class_common *stream_class,
149 bt_ctf_visitor visitor, void *data);
150
151BT_HIDDEN
152int bt_ctf_stream_class_visit(struct bt_ctf_stream_class *stream_class,
153 bt_ctf_visitor visitor, void *data);
154
155static inline
156struct bt_ctf_trace_common *bt_ctf_stream_class_common_borrow_trace(
157 struct bt_ctf_stream_class_common *stream_class)
158{
159 BT_ASSERT(stream_class);
160 return (void *) bt_object_borrow_parent(&stream_class->base);
161}
162
163static inline
164int bt_ctf_stream_class_common_set_name(struct bt_ctf_stream_class_common *stream_class,
165 const char *name)
166{
167 int ret = 0;
168
169 if (!stream_class) {
170 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
171 ret = -1;
172 goto end;
173 }
174
175 if (stream_class->frozen) {
176 BT_LOGW("Invalid parameter: stream class is frozen: "
177 "addr=%p, name=\"%s\", id=%" PRId64,
178 stream_class,
179 bt_ctf_stream_class_common_get_name(stream_class),
180 bt_ctf_stream_class_common_get_id(stream_class));
181 ret = -1;
182 goto end;
183 }
184
185 if (!name) {
186 g_string_assign(stream_class->name, "");
187 } else {
188 if (strlen(name) == 0) {
189 BT_LOGW("Invalid parameter: name is empty.");
190 ret = -1;
191 goto end;
192 }
193
194 g_string_assign(stream_class->name, name);
195 }
196
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));
201end:
202 return ret;
203}
204
205static inline
206void _bt_ctf_stream_class_common_set_id(
207 struct bt_ctf_stream_class_common *stream_class, int64_t id)
208{
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));
216}
217
218static inline
219int bt_ctf_stream_class_common_set_id_no_check(
220 struct bt_ctf_stream_class_common *stream_class, int64_t id)
221{
222 _bt_ctf_stream_class_common_set_id(stream_class, id);
223 return 0;
224}
225
226static inline
227int bt_ctf_stream_class_common_set_id(struct bt_ctf_stream_class_common *stream_class,
228 uint64_t id_param)
229{
230 int ret = 0;
231 int64_t id = (int64_t) id_param;
232
233 if (!stream_class) {
234 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
235 ret = -1;
236 goto end;
237 }
238
239 if (stream_class->frozen) {
240 BT_LOGW("Invalid parameter: stream class is frozen: "
241 "addr=%p, name=\"%s\", id=%" PRId64,
242 stream_class,
243 bt_ctf_stream_class_common_get_name(stream_class),
244 bt_ctf_stream_class_common_get_id(stream_class));
245 ret = -1;
246 goto end;
247 }
248
249 if (id < 0) {
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,
253 stream_class,
254 bt_ctf_stream_class_common_get_name(stream_class),
255 bt_ctf_stream_class_common_get_id(stream_class),
256 id_param);
257 ret = -1;
258 goto end;
259 }
260
261 ret = bt_ctf_stream_class_common_set_id_no_check(stream_class, id);
262 if (ret == 0) {
263 BT_LOGV("Set stream class's ID: "
264 "addr=%p, name=\"%s\", id=%" PRId64,
265 stream_class,
266 bt_ctf_stream_class_common_get_name(stream_class),
267 bt_ctf_stream_class_common_get_id(stream_class));
268 }
269end:
270 return ret;
271}
272
273static inline
274int64_t bt_ctf_stream_class_common_get_event_class_count(
275 struct bt_ctf_stream_class_common *stream_class)
276{
277 int64_t ret;
278
279 if (!stream_class) {
280 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
281 ret = (int64_t) -1;
282 goto end;
283 }
284
285 ret = (int64_t) stream_class->event_classes->len;
286end:
287 return ret;
288}
289
290static inline
291struct 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)
293{
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 ", "
297 "count=%u",
298 index, stream_class->event_classes->len);
299 return g_ptr_array_index(stream_class->event_classes, index);
300}
301
302static inline
303struct 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)
305{
306 int64_t id_key = (int64_t) id;
307
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,
312 &id_key);
313}
314
315static inline
316struct bt_ctf_field_type_common *
317bt_ctf_stream_class_common_borrow_packet_context_field_type(
318 struct bt_ctf_stream_class_common *stream_class)
319{
320 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
321 return stream_class->packet_context_field_type;
322}
323
324static inline
325int 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)
328{
329 int ret = 0;
330
331 if (!stream_class) {
332 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
333 ret = -1;
334 goto end;
335 }
336
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));
342 ret = -1;
343 goto end;
344 }
345
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),
355 packet_context_type,
356 bt_ctf_field_type_id_string(
357 bt_ctf_field_type_common_get_type_id(packet_context_type)));
358 ret = -1;
359 goto end;
360 }
361
362 bt_put(stream_class->packet_context_field_type);
363 bt_get(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);
371
372end:
373 return ret;
374}
375
376static inline
377struct bt_ctf_field_type_common *
378bt_ctf_stream_class_common_borrow_event_header_field_type(
379 struct bt_ctf_stream_class_common *stream_class)
380{
381 struct bt_ctf_field_type_common *ret = NULL;
382
383 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
384
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,
388 stream_class,
389 bt_ctf_stream_class_common_get_name(stream_class),
390 bt_ctf_stream_class_common_get_id(stream_class));
391 goto end;
392 }
393
394 ret = stream_class->event_header_field_type;
395
396end:
397 return ret;
398}
399
400static inline
401int 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)
404{
405 int ret = 0;
406
407 if (!stream_class) {
408 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
409 ret = -1;
410 goto end;
411 }
412
413 if (stream_class->frozen) {
414 BT_LOGW("Invalid parameter: stream class is frozen: "
415 "addr=%p, name=\"%s\", id=%" PRId64,
416 stream_class,
417 bt_ctf_stream_class_common_get_name(stream_class),
418 bt_ctf_stream_class_common_get_id(stream_class));
419 ret = -1;
420 goto end;
421 }
422
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),
432 event_header_type,
433 bt_ctf_field_type_id_string(
434 bt_ctf_field_type_common_get_type_id(event_header_type)));
435 ret = -1;
436 goto end;
437 }
438
439 bt_put(stream_class->event_header_field_type);
440 stream_class->event_header_field_type = bt_get(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),
446 event_header_type);
447end:
448 return ret;
449}
450
451static inline
452struct bt_ctf_field_type_common *
453bt_ctf_stream_class_common_borrow_event_context_field_type(
454 struct bt_ctf_stream_class_common *stream_class)
455{
456 struct bt_ctf_field_type_common *ret = NULL;
457
458 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
459
460 if (!stream_class->event_context_field_type) {
461 goto end;
462 }
463
464 ret = stream_class->event_context_field_type;
465
466end:
467 return ret;
468}
469
470static inline
471int 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)
474{
475 int ret = 0;
476
477 if (!stream_class) {
478 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
479 ret = -1;
480 goto end;
481 }
482
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));
488 ret = -1;
489 goto end;
490 }
491
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),
501 event_context_type,
502 bt_ctf_field_type_id_string(
503 bt_ctf_field_type_common_get_type_id(event_context_type)));
504 ret = -1;
505 goto end;
506 }
507
508 bt_put(stream_class->event_context_field_type);
509 stream_class->event_context_field_type = bt_get(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),
515 event_context_type);
516end:
517 return ret;
518}
3dca2276
PP
519
520struct bt_ctf_stream_class {
16ca5ff0 521 struct bt_ctf_stream_class_common common;
3dca2276
PP
522 struct bt_ctf_clock *clock;
523 int64_t next_stream_id;
524};
525
526struct metadata_context;
527
528BT_HIDDEN
529int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
530 struct metadata_context *context);
531
532BT_HIDDEN
533int 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);
537
538#endif /* BABELTRACE_CTF_WRITER_STREAM_CLASS_INTERNAL_H */
This page took 0.043043 seconds and 4 git commands to generate.