409c5ab0f21430dea3402a8990fc1e6568b11cbb
[babeltrace.git] / formats / ctf / ir / stream-class.c
1 /*
2 * stream-class.c
3 *
4 * Babeltrace CTF IR - Stream Class
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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>
38 #include <babeltrace/ctf-ir/utils.h>
39 #include <babeltrace/compiler.h>
40 #include <babeltrace/align.h>
41
42 static
43 void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref);
44 static
45 int init_event_header(struct bt_ctf_stream_class *stream_class);
46 static
47 int init_packet_context(struct bt_ctf_stream_class *stream_class);
48
49 struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
50 {
51 int ret;
52 struct bt_ctf_stream_class *stream_class = NULL;
53
54 if (name && bt_ctf_validate_identifier(name)) {
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
70 ret = init_event_header(stream_class);
71 if (ret) {
72 goto error_destroy;
73 }
74
75 ret = init_packet_context(stream_class);
76 if (ret) {
77 goto error_destroy;
78 }
79
80 bt_ctf_ref_init(&stream_class->ref_count);
81 return stream_class;
82
83 error_destroy:
84 bt_ctf_stream_class_destroy(&stream_class->ref_count);
85 stream_class = NULL;
86 error:
87 return stream_class;
88 }
89
90 const 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;
100 end:
101 return name;
102 }
103
104 int 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);
115 end:
116 return ret;
117 }
118
119 struct 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);
130 end:
131 return clock;
132 }
133
134 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
135 struct bt_ctf_clock *clock)
136 {
137 int ret = 0;
138 struct bt_ctf_field_type *timestamp_field = NULL;
139
140 if (!stream_class || !clock || stream_class->frozen) {
141 ret = -1;
142 goto end;
143 }
144
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
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);
175 end:
176 if (timestamp_field) {
177 bt_ctf_field_type_put(timestamp_field);
178 }
179 return ret;
180 }
181
182 int64_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;
192 end:
193 return ret;
194 }
195
196 BT_HIDDEN
197 int _bt_ctf_stream_class_set_id(
198 struct bt_ctf_stream_class *stream_class, uint32_t id)
199 {
200 stream_class->id = id;
201 stream_class->id_set = 1;
202 return 0;
203 }
204
205 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
206 uint32_t id)
207 {
208 int ret = 0;
209
210 if (!stream_class || stream_class->frozen) {
211 ret = -1;
212 goto end;
213 }
214
215 ret = _bt_ctf_stream_class_set_id(stream_class, id);
216 if (ret) {
217 goto end;
218 }
219 end:
220 return ret;
221 }
222
223 int bt_ctf_stream_class_add_event_class(
224 struct bt_ctf_stream_class *stream_class,
225 struct bt_ctf_event_class *event_class)
226 {
227 int ret = 0;
228 int64_t event_id;
229
230 if (!stream_class || !event_class) {
231 ret = -1;
232 goto end;
233 }
234
235 /* Check for duplicate event classes */
236 struct search_query query = { .value = event_class, .found = 0 };
237 g_ptr_array_foreach(stream_class->event_classes, value_exists, &query);
238 if (query.found) {
239 ret = -1;
240 goto end;
241 }
242
243 /* Only set an event id if none was explicitly set before */
244 event_id = bt_ctf_event_class_get_id(event_class);
245 if (event_id < 0) {
246 if (bt_ctf_event_class_set_id(event_class,
247 stream_class->next_event_id++)) {
248 ret = -1;
249 goto end;
250 }
251 }
252
253 ret = bt_ctf_event_class_set_stream_class(event_class, stream_class);
254 if (ret) {
255 goto end;
256 }
257
258 bt_ctf_event_class_get(event_class);
259 g_ptr_array_add(stream_class->event_classes, event_class);
260 bt_ctf_event_class_freeze(event_class);
261
262 if (stream_class->byte_order) {
263 /*
264 * Only set native byte order if it has been initialized
265 * when the stream class was added to a trace.
266 *
267 * If not set here, this will be set when the stream
268 * classe will be added to a trace.
269 */
270 bt_ctf_event_class_set_native_byte_order(event_class,
271 stream_class->byte_order);
272 }
273 end:
274 return ret;
275 }
276
277 int bt_ctf_stream_class_get_event_class_count(
278 struct bt_ctf_stream_class *stream_class)
279 {
280 int ret;
281
282 if (!stream_class) {
283 ret = -1;
284 goto end;
285 }
286
287 ret = (int) stream_class->event_classes->len;
288 end:
289 return ret;
290 }
291
292 struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
293 struct bt_ctf_stream_class *stream_class, int index)
294 {
295 struct bt_ctf_event_class *event_class = NULL;
296
297 if (!stream_class || index < 0 ||
298 index >= stream_class->event_classes->len) {
299 goto end;
300 }
301
302 event_class = g_ptr_array_index(stream_class->event_classes, index);
303 bt_ctf_event_class_get(event_class);
304 end:
305 return event_class;
306 }
307
308 struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
309 struct bt_ctf_stream_class *stream_class, const char *name)
310 {
311 size_t i;
312 GQuark name_quark;
313 struct bt_ctf_event_class *event_class = NULL;
314
315 if (!stream_class || !name) {
316 goto end;
317 }
318
319 name_quark = g_quark_try_string(name);
320 if (!name_quark) {
321 goto end;
322 }
323
324 for (i = 0; i < stream_class->event_classes->len; i++) {
325 struct bt_ctf_event_class *current_event_class =
326 g_ptr_array_index(stream_class->event_classes, i);
327
328 if (name_quark == current_event_class->name) {
329 event_class = current_event_class;
330 bt_ctf_event_class_get(event_class);
331 goto end;
332 }
333 }
334 end:
335 return event_class;
336 }
337
338 struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id(
339 struct bt_ctf_stream_class *stream_class, uint32_t id)
340 {
341 size_t i;
342 struct bt_ctf_event_class *event_class = NULL;
343
344 if (!stream_class) {
345 goto end;
346 }
347
348 for (i = 0; i < stream_class->event_classes->len; i++) {
349 struct bt_ctf_event_class *current_event_class =
350 g_ptr_array_index(stream_class->event_classes, i);
351
352 if (bt_ctf_event_class_get_id(current_event_class) == id) {
353 event_class = current_event_class;
354 bt_ctf_event_class_get(event_class);
355 goto end;
356 }
357 }
358 end:
359 return event_class;
360 }
361
362 struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
363 struct bt_ctf_stream_class *stream_class)
364 {
365 struct bt_ctf_field_type *ret = NULL;
366
367 if (!stream_class) {
368 goto end;
369 }
370
371 assert(stream_class->packet_context_type);
372 bt_ctf_field_type_get(stream_class->packet_context_type);
373 ret = stream_class->packet_context_type;
374 end:
375 return ret;
376 }
377
378 int bt_ctf_stream_class_set_packet_context_type(
379 struct bt_ctf_stream_class *stream_class,
380 struct bt_ctf_field_type *packet_context_type)
381 {
382 int ret = 0;
383
384 if (!stream_class || !packet_context_type || stream_class->frozen) {
385 ret = -1;
386 goto end;
387 }
388
389 assert(stream_class->packet_context_type);
390 if (stream_class->packet_context_type == packet_context_type) {
391 goto end;
392 }
393 if (bt_ctf_field_type_get_type_id(packet_context_type) !=
394 CTF_TYPE_STRUCT) {
395 /* A packet context must be a structure */
396 ret = -1;
397 goto end;
398 }
399
400 bt_ctf_field_type_put(stream_class->packet_context_type);
401 bt_ctf_field_type_get(packet_context_type);
402 stream_class->packet_context_type = packet_context_type;
403 end:
404 return ret;
405 }
406
407 struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type(
408 struct bt_ctf_stream_class *stream_class)
409 {
410 struct bt_ctf_field_type *ret = NULL;
411
412 if (!stream_class || !stream_class->event_header_type) {
413 goto end;
414 }
415
416 assert(stream_class->event_header_type);
417 bt_ctf_field_type_get(stream_class->event_header_type);
418 ret = stream_class->event_header_type;
419 end:
420 return ret;
421 }
422
423 int bt_ctf_stream_class_set_event_header_type(
424 struct bt_ctf_stream_class *stream_class,
425 struct bt_ctf_field_type *event_header_type)
426 {
427 int ret = 0;
428
429 if (!stream_class || !event_header_type || stream_class->frozen) {
430 ret = -1;
431 goto end;
432 }
433
434 assert(stream_class->event_header_type);
435 if (stream_class->event_header_type == event_header_type) {
436 goto end;
437 }
438 if (bt_ctf_field_type_get_type_id(event_header_type) !=
439 CTF_TYPE_STRUCT) {
440 /* An event header must be a structure */
441 ret = -1;
442 goto end;
443 }
444
445 bt_ctf_field_type_put(stream_class->event_header_type);
446 bt_ctf_field_type_get(event_header_type);
447 stream_class->event_header_type = event_header_type;
448 end:
449 return ret;
450 }
451
452 struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
453 struct bt_ctf_stream_class *stream_class)
454 {
455 struct bt_ctf_field_type *ret = NULL;
456
457 if (!stream_class || !stream_class->event_context_type) {
458 goto end;
459 }
460
461 assert(stream_class->event_context_type);
462 bt_ctf_field_type_get(stream_class->event_context_type);
463 ret = stream_class->event_context_type;
464 end:
465 return ret;
466 }
467
468 int bt_ctf_stream_class_set_event_context_type(
469 struct bt_ctf_stream_class *stream_class,
470 struct bt_ctf_field_type *event_context_type)
471 {
472 int ret = 0;
473
474 if (!stream_class || !event_context_type || stream_class->frozen) {
475 ret = -1;
476 goto end;
477 }
478
479 if (bt_ctf_field_type_get_type_id(event_context_type) !=
480 CTF_TYPE_STRUCT) {
481 /* A packet context must be a structure */
482 ret = -1;
483 goto end;
484 }
485
486 bt_ctf_field_type_put(stream_class->event_context_type);
487 bt_ctf_field_type_get(event_context_type);
488 stream_class->event_context_type = event_context_type;
489 end:
490 return ret;
491 }
492
493 void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
494 {
495 if (!stream_class) {
496 return;
497 }
498
499 bt_ctf_ref_get(&stream_class->ref_count);
500 }
501
502 void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
503 {
504 if (!stream_class) {
505 return;
506 }
507
508 bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy);
509 }
510
511 BT_HIDDEN
512 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
513 {
514 if (!stream_class) {
515 return;
516 }
517
518 stream_class->frozen = 1;
519 bt_ctf_field_type_freeze(stream_class->event_header_type);
520 bt_ctf_field_type_freeze(stream_class->packet_context_type);
521 bt_ctf_field_type_freeze(stream_class->event_context_type);
522 bt_ctf_clock_freeze(stream_class->clock);
523 }
524
525 BT_HIDDEN
526 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
527 enum bt_ctf_byte_order byte_order)
528 {
529 int i, ret = 0;
530 int internal_byte_order;
531
532 /* Note that "NATIVE" means the trace's endianness, not the host's. */
533 if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN ||
534 byte_order > BT_CTF_BYTE_ORDER_NETWORK) {
535 ret = -1;
536 goto end;
537 }
538
539 switch (byte_order) {
540 case BT_CTF_BYTE_ORDER_NETWORK:
541 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
542 internal_byte_order = BIG_ENDIAN;
543 break;
544 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
545 internal_byte_order = LITTLE_ENDIAN;
546 break;
547 default:
548 ret = -1;
549 goto end;
550 }
551
552 stream_class->byte_order = internal_byte_order;
553
554 /* Set native byte order to little or big endian */
555 bt_ctf_field_type_set_native_byte_order(
556 stream_class->event_header_type, stream_class->byte_order);
557 bt_ctf_field_type_set_native_byte_order(
558 stream_class->packet_context_type, stream_class->byte_order);
559 bt_ctf_field_type_set_native_byte_order(
560 stream_class->event_context_type, stream_class->byte_order);
561
562 /* Set all events' native byte order */
563 for (i = 0; i < stream_class->event_classes->len; i++) {
564 bt_ctf_event_class_set_native_byte_order(
565 g_ptr_array_index(stream_class->event_classes, i),
566 stream_class->byte_order);
567 bt_ctf_event_class_freeze(
568 g_ptr_array_index(stream_class->event_classes, i));
569 }
570 end:
571 return ret;
572 }
573
574 BT_HIDDEN
575 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
576 struct metadata_context *context)
577 {
578 int64_t ret = 0;
579 size_t i;
580
581 g_string_assign(context->field_name, "");
582 context->current_indentation_level = 1;
583 if (!stream_class->id_set) {
584 ret = -1;
585 goto end;
586 }
587
588 g_string_append_printf(context->string,
589 "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ",
590 stream_class->id);
591 ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
592 context);
593 if (ret) {
594 goto end;
595 }
596
597 g_string_append(context->string, ";\n\n\tpacket.context := ");
598 ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
599 context);
600 if (ret) {
601 goto end;
602 }
603
604 if (stream_class->event_context_type) {
605 g_string_append(context->string, ";\n\n\tevent.context := ");
606 ret = bt_ctf_field_type_serialize(
607 stream_class->event_context_type, context);
608 if (ret) {
609 goto end;
610 }
611 }
612
613 g_string_append(context->string, ";\n};\n\n");
614 for (i = 0; i < stream_class->event_classes->len; i++) {
615 struct bt_ctf_event_class *event_class =
616 stream_class->event_classes->pdata[i];
617
618 ret = bt_ctf_event_class_serialize(event_class, context);
619 if (ret) {
620 goto end;
621 }
622 }
623 end:
624 context->current_indentation_level = 0;
625 return ret;
626 }
627
628 static
629 void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
630 {
631 struct bt_ctf_stream_class *stream_class;
632
633 if (!ref) {
634 return;
635 }
636
637 stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count);
638 bt_ctf_clock_put(stream_class->clock);
639
640 if (stream_class->event_classes) {
641 size_t i;
642
643 /* Unregister this stream class from the event classes */
644 for (i = 0; i < stream_class->event_classes->len; i++) {
645 struct bt_ctf_event_class *event_class =
646 g_ptr_array_index(stream_class->event_classes,
647 i);
648
649 bt_ctf_event_class_set_stream_class(event_class, NULL);
650 }
651
652 g_ptr_array_free(stream_class->event_classes, TRUE);
653 }
654
655 if (stream_class->name) {
656 g_string_free(stream_class->name, TRUE);
657 }
658
659 bt_ctf_field_type_put(stream_class->event_header_type);
660 bt_ctf_field_type_put(stream_class->packet_context_type);
661 if (stream_class->event_context_type) {
662 bt_ctf_field_type_put(stream_class->event_context_type);
663 }
664 g_free(stream_class);
665 }
666
667 static
668 int init_event_header(struct bt_ctf_stream_class *stream_class)
669 {
670 int ret = 0;
671 struct bt_ctf_field_type *event_header_type =
672 bt_ctf_field_type_structure_create();
673 struct bt_ctf_field_type *_uint32_t =
674 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
675 struct bt_ctf_field_type *_uint64_t =
676 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
677
678 if (!event_header_type) {
679 ret = -1;
680 goto end;
681 }
682
683 ret = bt_ctf_field_type_structure_add_field(event_header_type,
684 _uint32_t, "id");
685 if (ret) {
686 goto end;
687 }
688
689 ret = bt_ctf_field_type_structure_add_field(event_header_type,
690 _uint64_t, "timestamp");
691 if (ret) {
692 goto end;
693 }
694
695 if (stream_class->event_header_type) {
696 bt_ctf_field_type_put(stream_class->event_header_type);
697 }
698 stream_class->event_header_type = event_header_type;
699 end:
700 if (ret) {
701 bt_ctf_field_type_put(event_header_type);
702 }
703
704 bt_ctf_field_type_put(_uint32_t);
705 bt_ctf_field_type_put(_uint64_t);
706 return ret;
707 }
708
709 static
710 int init_packet_context(struct bt_ctf_stream_class *stream_class)
711 {
712 int ret = 0;
713 struct bt_ctf_field_type *packet_context_type =
714 bt_ctf_field_type_structure_create();
715 struct bt_ctf_field_type *_uint64_t =
716 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
717
718 if (!packet_context_type) {
719 ret = -1;
720 goto end;
721 }
722
723 /*
724 * We create a stream packet context as proposed in the CTF
725 * specification.
726 */
727 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
728 _uint64_t, "timestamp_begin");
729 if (ret) {
730 goto end;
731 }
732
733 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
734 _uint64_t, "timestamp_end");
735 if (ret) {
736 goto end;
737 }
738
739 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
740 _uint64_t, "content_size");
741 if (ret) {
742 goto end;
743 }
744
745 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
746 _uint64_t, "packet_size");
747 if (ret) {
748 goto end;
749 }
750
751 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
752 _uint64_t, "events_discarded");
753 if (ret) {
754 goto end;
755 }
756
757 if (stream_class->packet_context_type) {
758 bt_ctf_field_type_put(stream_class->packet_context_type);
759 }
760 stream_class->packet_context_type = packet_context_type;
761 end:
762 if (ret) {
763 bt_ctf_field_type_put(packet_context_type);
764 goto end;
765 }
766
767 bt_ctf_field_type_put(_uint64_t);
768 return ret;
769 }
This page took 0.043604 seconds and 3 git commands to generate.