Tests: ensure that an event may be instanciated before its stream
[babeltrace.git] / formats / ctf / ir / stream-class.c
CommitLineData
11b0cdc8 1/*
3f043b05 2 * stream-class.c
11b0cdc8 3 *
d2dc44b6 4 * Babeltrace CTF IR - Stream Class
11b0cdc8 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
11b0cdc8
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29#include <babeltrace/ctf-writer/clock.h>
30#include <babeltrace/ctf-ir/clock-internal.h>
31#include <babeltrace/ctf-writer/event.h>
32#include <babeltrace/ctf-ir/event-internal.h>
33#include <babeltrace/ctf-ir/event-types-internal.h>
34#include <babeltrace/ctf-ir/event-fields-internal.h>
35#include <babeltrace/ctf-writer/stream.h>
36#include <babeltrace/ctf-ir/stream-class-internal.h>
37#include <babeltrace/ctf-writer/functor-internal.h>
654c1444 38#include <babeltrace/ctf-ir/utils.h>
11b0cdc8
JG
39#include <babeltrace/compiler.h>
40#include <babeltrace/align.h>
41
42static
43void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref);
44static
662e778c 45int init_event_header(struct bt_ctf_stream_class *stream_class);
11b0cdc8 46static
662e778c 47int init_packet_context(struct bt_ctf_stream_class *stream_class);
11b0cdc8
JG
48
49struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
50{
12c8a1a3 51 int ret;
11b0cdc8
JG
52 struct bt_ctf_stream_class *stream_class = NULL;
53
3ea33115 54 if (name && bt_ctf_validate_identifier(name)) {
11b0cdc8
JG
55 goto error;
56 }
57
58 stream_class = g_new0(struct bt_ctf_stream_class, 1);
59 if (!stream_class) {
60 goto error;
61 }
62
63 stream_class->name = g_string_new(name);
64 stream_class->event_classes = g_ptr_array_new_with_free_func(
65 (GDestroyNotify)bt_ctf_event_class_put);
66 if (!stream_class->event_classes) {
67 goto error_destroy;
68 }
69
662e778c
JG
70 ret = init_event_header(stream_class);
71 if (ret) {
72 goto error_destroy;
73 }
74
75 ret = init_packet_context(stream_class);
12c8a1a3
JG
76 if (ret) {
77 goto error_destroy;
78 }
79
11b0cdc8
JG
80 bt_ctf_ref_init(&stream_class->ref_count);
81 return stream_class;
82
83error_destroy:
84 bt_ctf_stream_class_destroy(&stream_class->ref_count);
85 stream_class = NULL;
86error:
87 return stream_class;
88}
89
69dc4535
JG
90const char *bt_ctf_stream_class_get_name(
91 struct bt_ctf_stream_class *stream_class)
92{
93 const char *name = NULL;
94
95 if (!stream_class) {
96 goto end;
97 }
98
99 name = stream_class->name->str;
100end:
101 return name;
102}
103
3ea33115
JG
104int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class,
105 const char *name)
106{
107 int ret = 0;
108
109 if (!stream_class || stream_class->frozen) {
110 ret = -1;
111 goto end;
112 }
113
114 g_string_assign(stream_class->name, name);
115end:
116 return ret;
117}
118
2f100782
JG
119struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
120 struct bt_ctf_stream_class *stream_class)
121{
122 struct bt_ctf_clock *clock = NULL;
123
124 if (!stream_class || !stream_class->clock) {
125 goto end;
126 }
127
128 clock = stream_class->clock;
129 bt_ctf_clock_get(clock);
130end:
131 return clock;
132}
133
11b0cdc8
JG
134int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
135 struct bt_ctf_clock *clock)
136{
137 int ret = 0;
eee752e5 138 struct bt_ctf_field_type *timestamp_field = NULL;
11b0cdc8
JG
139
140 if (!stream_class || !clock || stream_class->frozen) {
141 ret = -1;
142 goto end;
143 }
144
eee752e5
JG
145 /*
146 * Look for a "timestamp" field in the stream class' event header type
147 * and map the stream's clock to that field if no current mapping is
148 * currently set.
149 */
150 timestamp_field = bt_ctf_field_type_structure_get_field_type_by_name(
151 stream_class->event_header_type, "timestamp");
152 if (timestamp_field) {
153 struct bt_ctf_clock *mapped_clock;
154
155 mapped_clock = bt_ctf_field_type_integer_get_mapped_clock(
156 timestamp_field);
157 if (mapped_clock) {
158 bt_ctf_clock_put(mapped_clock);
159 goto end;
160 }
161
162 ret = bt_ctf_field_type_integer_set_mapped_clock(
163 timestamp_field, clock);
164 if (ret) {
165 goto end;
166 }
167 }
168
11b0cdc8
JG
169 if (stream_class->clock) {
170 bt_ctf_clock_put(stream_class->clock);
171 }
172
173 stream_class->clock = clock;
174 bt_ctf_clock_get(clock);
175end:
eee752e5
JG
176 if (timestamp_field) {
177 bt_ctf_field_type_put(timestamp_field);
178 }
11b0cdc8
JG
179 return ret;
180}
181
2f100782
JG
182int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class)
183{
184 int64_t ret;
185
186 if (!stream_class || !stream_class->id_set) {
187 ret = -1;
188 goto end;
189 }
190
191 ret = (int64_t) stream_class->id;
192end:
193 return ret;
194}
195
196int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
197 uint32_t id)
198{
199 int ret = 0;
200
739a93c7 201 if (!stream_class || stream_class->frozen) {
2f100782
JG
202 ret = -1;
203 goto end;
204 }
205
206 stream_class->id = id;
207 stream_class->id_set = 1;
208end:
209 return ret;
210}
211
11b0cdc8
JG
212int bt_ctf_stream_class_add_event_class(
213 struct bt_ctf_stream_class *stream_class,
214 struct bt_ctf_event_class *event_class)
215{
216 int ret = 0;
2f100782 217 int64_t event_id;
11b0cdc8
JG
218
219 if (!stream_class || !event_class) {
220 ret = -1;
221 goto end;
222 }
223
224 /* Check for duplicate event classes */
225 struct search_query query = { .value = event_class, .found = 0 };
226 g_ptr_array_foreach(stream_class->event_classes, value_exists, &query);
227 if (query.found) {
228 ret = -1;
229 goto end;
230 }
231
2f100782
JG
232 /* Only set an event id if none was explicitly set before */
233 event_id = bt_ctf_event_class_get_id(event_class);
234 if (event_id < 0) {
235 if (bt_ctf_event_class_set_id(event_class,
236 stream_class->next_event_id++)) {
237 ret = -1;
238 goto end;
239 }
240 }
241
242 ret = bt_ctf_event_class_set_stream_class(event_class, stream_class);
243 if (ret) {
11b0cdc8
JG
244 goto end;
245 }
246
247 bt_ctf_event_class_get(event_class);
248 g_ptr_array_add(stream_class->event_classes, event_class);
249end:
250 return ret;
251}
252
074ee56d 253int bt_ctf_stream_class_get_event_class_count(
69dc4535
JG
254 struct bt_ctf_stream_class *stream_class)
255{
074ee56d 256 int ret;
69dc4535
JG
257
258 if (!stream_class) {
259 ret = -1;
260 goto end;
261 }
262
074ee56d 263 ret = (int) stream_class->event_classes->len;
69dc4535
JG
264end:
265 return ret;
266}
267
268struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
074ee56d 269 struct bt_ctf_stream_class *stream_class, int index)
69dc4535
JG
270{
271 struct bt_ctf_event_class *event_class = NULL;
272
074ee56d
JG
273 if (!stream_class || index < 0 ||
274 index >= stream_class->event_classes->len) {
69dc4535
JG
275 goto end;
276 }
277
278 event_class = g_ptr_array_index(stream_class->event_classes, index);
279 bt_ctf_event_class_get(event_class);
280end:
281 return event_class;
282}
283
284struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
285 struct bt_ctf_stream_class *stream_class, const char *name)
286{
287 size_t i;
288 GQuark name_quark;
289 struct bt_ctf_event_class *event_class = NULL;
290
291 if (!stream_class || !name) {
292 goto end;
293 }
294
295 name_quark = g_quark_try_string(name);
296 if (!name_quark) {
297 goto end;
298 }
299
300 for (i = 0; i < stream_class->event_classes->len; i++) {
301 struct bt_ctf_event_class *current_event_class =
302 g_ptr_array_index(stream_class->event_classes, i);
303
304 if (name_quark == current_event_class->name) {
305 event_class = current_event_class;
306 bt_ctf_event_class_get(event_class);
307 goto end;
308 }
309 }
310end:
311 return event_class;
312}
313
12c8a1a3
JG
314struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
315 struct bt_ctf_stream_class *stream_class)
316{
317 struct bt_ctf_field_type *ret = NULL;
318
319 if (!stream_class) {
320 goto end;
321 }
322
323 assert(stream_class->packet_context_type);
324 bt_ctf_field_type_get(stream_class->packet_context_type);
325 ret = stream_class->packet_context_type;
326end:
327 return ret;
328}
329
330int bt_ctf_stream_class_set_packet_context_type(
331 struct bt_ctf_stream_class *stream_class,
332 struct bt_ctf_field_type *packet_context_type)
333{
334 int ret = 0;
335
0a00bfa1 336 if (!stream_class || !packet_context_type || stream_class->frozen) {
12c8a1a3
JG
337 ret = -1;
338 goto end;
339 }
340
341 assert(stream_class->packet_context_type);
662e778c
JG
342 if (stream_class->packet_context_type == packet_context_type) {
343 goto end;
344 }
b34f4d90 345 if (bt_ctf_field_type_get_type_id(packet_context_type) !=
12c8a1a3
JG
346 CTF_TYPE_STRUCT) {
347 /* A packet context must be a structure */
348 ret = -1;
349 goto end;
350 }
351
352 bt_ctf_field_type_put(stream_class->packet_context_type);
353 bt_ctf_field_type_get(packet_context_type);
354 stream_class->packet_context_type = packet_context_type;
355end:
356 return ret;
357}
358
662e778c
JG
359struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type(
360 struct bt_ctf_stream_class *stream_class)
361{
362 struct bt_ctf_field_type *ret = NULL;
363
364 if (!stream_class || !stream_class->event_header_type) {
365 goto end;
366 }
367
368 assert(stream_class->event_header_type);
369 bt_ctf_field_type_get(stream_class->event_header_type);
370 ret = stream_class->event_header_type;
371end:
372 return ret;
373}
374
375int bt_ctf_stream_class_set_event_header_type(
376 struct bt_ctf_stream_class *stream_class,
377 struct bt_ctf_field_type *event_header_type)
378{
379 int ret = 0;
380
381 if (!stream_class || !event_header_type || stream_class->frozen) {
382 ret = -1;
383 goto end;
384 }
385
386 assert(stream_class->event_header_type);
387 if (stream_class->event_header_type == event_header_type) {
388 goto end;
389 }
390 if (bt_ctf_field_type_get_type_id(event_header_type) !=
391 CTF_TYPE_STRUCT) {
392 /* An event header must be a structure */
393 ret = -1;
394 goto end;
395 }
396
397 bt_ctf_field_type_put(stream_class->event_header_type);
398 bt_ctf_field_type_get(event_header_type);
399 stream_class->event_header_type = event_header_type;
400end:
401 return ret;
402}
403
af181248
JG
404struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
405 struct bt_ctf_stream_class *stream_class)
406{
407 struct bt_ctf_field_type *ret = NULL;
408
409 if (!stream_class || !stream_class->event_context_type) {
410 goto end;
411 }
412
413 assert(stream_class->event_context_type);
414 bt_ctf_field_type_get(stream_class->event_context_type);
415 ret = stream_class->event_context_type;
416end:
417 return ret;
418}
419
420int bt_ctf_stream_class_set_event_context_type(
421 struct bt_ctf_stream_class *stream_class,
422 struct bt_ctf_field_type *event_context_type)
423{
424 int ret = 0;
425
426 if (!stream_class || !event_context_type || stream_class->frozen) {
427 ret = -1;
428 goto end;
429 }
430
431 if (bt_ctf_field_type_get_type_id(event_context_type) !=
432 CTF_TYPE_STRUCT) {
433 /* A packet context must be a structure */
434 ret = -1;
435 goto end;
436 }
437
438 bt_ctf_field_type_put(stream_class->event_context_type);
439 bt_ctf_field_type_get(event_context_type);
440 stream_class->event_context_type = event_context_type;
441end:
442 return ret;
443}
444
11b0cdc8
JG
445void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
446{
447 if (!stream_class) {
448 return;
449 }
450
451 bt_ctf_ref_get(&stream_class->ref_count);
452}
453
454void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
455{
456 if (!stream_class) {
457 return;
458 }
459
460 bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy);
461}
462
463BT_HIDDEN
464void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
465{
c35a1669
JG
466 size_t i;
467
11b0cdc8
JG
468 if (!stream_class) {
469 return;
470 }
471
472 stream_class->frozen = 1;
662e778c 473 bt_ctf_field_type_freeze(stream_class->event_header_type);
12c8a1a3 474 bt_ctf_field_type_freeze(stream_class->packet_context_type);
af181248 475 bt_ctf_field_type_freeze(stream_class->event_context_type);
11b0cdc8 476 bt_ctf_clock_freeze(stream_class->clock);
c35a1669
JG
477
478 bt_ctf_field_type_set_native_byte_order(
479 stream_class->event_header_type, stream_class->byte_order);
480 bt_ctf_field_type_set_native_byte_order(
481 stream_class->packet_context_type, stream_class->byte_order);
482 bt_ctf_field_type_set_native_byte_order(
483 stream_class->event_context_type, stream_class->byte_order);
484 for (i = 0; i < stream_class->event_classes->len; i++) {
485 bt_ctf_event_class_set_native_byte_order(
486 g_ptr_array_index(stream_class->event_classes, i),
487 stream_class->byte_order);
488 bt_ctf_event_class_freeze(
489 g_ptr_array_index(stream_class->event_classes, i));
490 }
11b0cdc8
JG
491}
492
11b0cdc8
JG
493BT_HIDDEN
494int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
495 enum bt_ctf_byte_order byte_order)
496{
497 int ret = 0;
c35a1669 498 int internal_byte_order;
11b0cdc8 499
c35a1669
JG
500 /* Note that "NATIVE" means the trace's endianness, not the host's. */
501 if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN ||
502 byte_order > BT_CTF_BYTE_ORDER_NETWORK ||
503 stream_class->frozen) {
504 ret = -1;
505 goto end;
506 }
507
508 switch (byte_order) {
509 case BT_CTF_BYTE_ORDER_NETWORK:
510 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
511 internal_byte_order = BIG_ENDIAN;
512 break;
513 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
514 internal_byte_order = LITTLE_ENDIAN;
515 break;
516 default:
517 ret = -1;
11b0cdc8
JG
518 goto end;
519 }
c35a1669
JG
520
521 stream_class->byte_order = internal_byte_order;
11b0cdc8
JG
522end:
523 return ret;
524}
525
526BT_HIDDEN
527int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
528 struct metadata_context *context)
529{
2f100782 530 int64_t ret = 0;
11b0cdc8
JG
531 size_t i;
532
533 g_string_assign(context->field_name, "");
534 context->current_indentation_level = 1;
535 if (!stream_class->id_set) {
536 ret = -1;
537 goto end;
538 }
539
540 g_string_append_printf(context->string,
541 "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ",
542 stream_class->id);
543 ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
544 context);
545 if (ret) {
546 goto end;
547 }
548
549 g_string_append(context->string, ";\n\n\tpacket.context := ");
550 ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
551 context);
552 if (ret) {
553 goto end;
554 }
555
556 if (stream_class->event_context_type) {
557 g_string_append(context->string, ";\n\n\tevent.context := ");
558 ret = bt_ctf_field_type_serialize(
559 stream_class->event_context_type, context);
560 if (ret) {
561 goto end;
562 }
563 }
564
565 g_string_append(context->string, ";\n};\n\n");
11b0cdc8
JG
566 for (i = 0; i < stream_class->event_classes->len; i++) {
567 struct bt_ctf_event_class *event_class =
568 stream_class->event_classes->pdata[i];
569
11b0cdc8
JG
570 ret = bt_ctf_event_class_serialize(event_class, context);
571 if (ret) {
572 goto end;
573 }
574 }
575end:
576 context->current_indentation_level = 0;
577 return ret;
578}
579
580static
581void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
582{
583 struct bt_ctf_stream_class *stream_class;
584
585 if (!ref) {
586 return;
587 }
588
589 stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count);
590 bt_ctf_clock_put(stream_class->clock);
591
592 if (stream_class->event_classes) {
2f100782
JG
593 size_t i;
594
595 /* Unregister this stream class from the event classes */
596 for (i = 0; i < stream_class->event_classes->len; i++) {
597 struct bt_ctf_event_class *event_class =
598 g_ptr_array_index(stream_class->event_classes,
599 i);
600
601 bt_ctf_event_class_set_stream_class(event_class, NULL);
602 }
603
11b0cdc8
JG
604 g_ptr_array_free(stream_class->event_classes, TRUE);
605 }
606
607 if (stream_class->name) {
608 g_string_free(stream_class->name, TRUE);
609 }
610
611 bt_ctf_field_type_put(stream_class->event_header_type);
11b0cdc8 612 bt_ctf_field_type_put(stream_class->packet_context_type);
af181248
JG
613 if (stream_class->event_context_type) {
614 bt_ctf_field_type_put(stream_class->event_context_type);
615 }
11b0cdc8
JG
616 g_free(stream_class);
617}
618
619static
662e778c 620int init_event_header(struct bt_ctf_stream_class *stream_class)
11b0cdc8
JG
621{
622 int ret = 0;
623 struct bt_ctf_field_type *event_header_type =
624 bt_ctf_field_type_structure_create();
625 struct bt_ctf_field_type *_uint32_t =
626 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
627 struct bt_ctf_field_type *_uint64_t =
628 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
629
630 if (!event_header_type) {
631 ret = -1;
632 goto end;
633 }
634
11b0cdc8
JG
635 ret = bt_ctf_field_type_structure_add_field(event_header_type,
636 _uint32_t, "id");
637 if (ret) {
638 goto end;
639 }
640
641 ret = bt_ctf_field_type_structure_add_field(event_header_type,
642 _uint64_t, "timestamp");
643 if (ret) {
644 goto end;
645 }
646
662e778c
JG
647 if (stream_class->event_header_type) {
648 bt_ctf_field_type_put(stream_class->event_header_type);
de876b7f 649 }
662e778c 650 stream_class->event_header_type = event_header_type;
11b0cdc8
JG
651end:
652 if (ret) {
653 bt_ctf_field_type_put(event_header_type);
654 }
655
656 bt_ctf_field_type_put(_uint32_t);
657 bt_ctf_field_type_put(_uint64_t);
658 return ret;
659}
660
661static
662e778c 662int init_packet_context(struct bt_ctf_stream_class *stream_class)
11b0cdc8
JG
663{
664 int ret = 0;
665 struct bt_ctf_field_type *packet_context_type =
666 bt_ctf_field_type_structure_create();
667 struct bt_ctf_field_type *_uint64_t =
668 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
669
670 if (!packet_context_type) {
671 ret = -1;
672 goto end;
673 }
674
675 /*
676 * We create a stream packet context as proposed in the CTF
677 * specification.
678 */
11b0cdc8
JG
679 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
680 _uint64_t, "timestamp_begin");
681 if (ret) {
682 goto end;
683 }
684
685 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
686 _uint64_t, "timestamp_end");
687 if (ret) {
688 goto end;
689 }
690
691 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
692 _uint64_t, "content_size");
693 if (ret) {
694 goto end;
695 }
696
697 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
698 _uint64_t, "packet_size");
699 if (ret) {
700 goto end;
701 }
702
703 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
704 _uint64_t, "events_discarded");
705 if (ret) {
706 goto end;
707 }
708
662e778c
JG
709 if (stream_class->packet_context_type) {
710 bt_ctf_field_type_put(stream_class->packet_context_type);
711 }
11b0cdc8 712 stream_class->packet_context_type = packet_context_type;
11b0cdc8
JG
713end:
714 if (ret) {
715 bt_ctf_field_type_put(packet_context_type);
716 goto end;
717 }
718
719 bt_ctf_field_type_put(_uint64_t);
720 return ret;
721}
This page took 0.052128 seconds and 4 git commands to generate.