Fix: Check stream fd value before closing
[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);
58203827 249 bt_ctf_event_class_freeze(event_class);
11b0cdc8
JG
250end:
251 return ret;
252}
253
074ee56d 254int bt_ctf_stream_class_get_event_class_count(
69dc4535
JG
255 struct bt_ctf_stream_class *stream_class)
256{
074ee56d 257 int ret;
69dc4535
JG
258
259 if (!stream_class) {
260 ret = -1;
261 goto end;
262 }
263
074ee56d 264 ret = (int) stream_class->event_classes->len;
69dc4535
JG
265end:
266 return ret;
267}
268
269struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
074ee56d 270 struct bt_ctf_stream_class *stream_class, int index)
69dc4535
JG
271{
272 struct bt_ctf_event_class *event_class = NULL;
273
074ee56d
JG
274 if (!stream_class || index < 0 ||
275 index >= stream_class->event_classes->len) {
69dc4535
JG
276 goto end;
277 }
278
279 event_class = g_ptr_array_index(stream_class->event_classes, index);
280 bt_ctf_event_class_get(event_class);
281end:
282 return event_class;
283}
284
285struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
286 struct bt_ctf_stream_class *stream_class, const char *name)
287{
288 size_t i;
289 GQuark name_quark;
290 struct bt_ctf_event_class *event_class = NULL;
291
292 if (!stream_class || !name) {
293 goto end;
294 }
295
296 name_quark = g_quark_try_string(name);
297 if (!name_quark) {
298 goto end;
299 }
300
301 for (i = 0; i < stream_class->event_classes->len; i++) {
302 struct bt_ctf_event_class *current_event_class =
303 g_ptr_array_index(stream_class->event_classes, i);
304
305 if (name_quark == current_event_class->name) {
306 event_class = current_event_class;
307 bt_ctf_event_class_get(event_class);
308 goto end;
309 }
310 }
311end:
312 return event_class;
313}
314
12c8a1a3
JG
315struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
316 struct bt_ctf_stream_class *stream_class)
317{
318 struct bt_ctf_field_type *ret = NULL;
319
320 if (!stream_class) {
321 goto end;
322 }
323
324 assert(stream_class->packet_context_type);
325 bt_ctf_field_type_get(stream_class->packet_context_type);
326 ret = stream_class->packet_context_type;
327end:
328 return ret;
329}
330
331int bt_ctf_stream_class_set_packet_context_type(
332 struct bt_ctf_stream_class *stream_class,
333 struct bt_ctf_field_type *packet_context_type)
334{
335 int ret = 0;
336
0a00bfa1 337 if (!stream_class || !packet_context_type || stream_class->frozen) {
12c8a1a3
JG
338 ret = -1;
339 goto end;
340 }
341
342 assert(stream_class->packet_context_type);
662e778c
JG
343 if (stream_class->packet_context_type == packet_context_type) {
344 goto end;
345 }
b34f4d90 346 if (bt_ctf_field_type_get_type_id(packet_context_type) !=
12c8a1a3
JG
347 CTF_TYPE_STRUCT) {
348 /* A packet context must be a structure */
349 ret = -1;
350 goto end;
351 }
352
353 bt_ctf_field_type_put(stream_class->packet_context_type);
354 bt_ctf_field_type_get(packet_context_type);
355 stream_class->packet_context_type = packet_context_type;
356end:
357 return ret;
358}
359
662e778c
JG
360struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type(
361 struct bt_ctf_stream_class *stream_class)
362{
363 struct bt_ctf_field_type *ret = NULL;
364
365 if (!stream_class || !stream_class->event_header_type) {
366 goto end;
367 }
368
369 assert(stream_class->event_header_type);
370 bt_ctf_field_type_get(stream_class->event_header_type);
371 ret = stream_class->event_header_type;
372end:
373 return ret;
374}
375
376int bt_ctf_stream_class_set_event_header_type(
377 struct bt_ctf_stream_class *stream_class,
378 struct bt_ctf_field_type *event_header_type)
379{
380 int ret = 0;
381
382 if (!stream_class || !event_header_type || stream_class->frozen) {
383 ret = -1;
384 goto end;
385 }
386
387 assert(stream_class->event_header_type);
388 if (stream_class->event_header_type == event_header_type) {
389 goto end;
390 }
391 if (bt_ctf_field_type_get_type_id(event_header_type) !=
392 CTF_TYPE_STRUCT) {
393 /* An event header must be a structure */
394 ret = -1;
395 goto end;
396 }
397
398 bt_ctf_field_type_put(stream_class->event_header_type);
399 bt_ctf_field_type_get(event_header_type);
400 stream_class->event_header_type = event_header_type;
401end:
402 return ret;
403}
404
af181248
JG
405struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
406 struct bt_ctf_stream_class *stream_class)
407{
408 struct bt_ctf_field_type *ret = NULL;
409
410 if (!stream_class || !stream_class->event_context_type) {
411 goto end;
412 }
413
414 assert(stream_class->event_context_type);
415 bt_ctf_field_type_get(stream_class->event_context_type);
416 ret = stream_class->event_context_type;
417end:
418 return ret;
419}
420
421int bt_ctf_stream_class_set_event_context_type(
422 struct bt_ctf_stream_class *stream_class,
423 struct bt_ctf_field_type *event_context_type)
424{
425 int ret = 0;
426
427 if (!stream_class || !event_context_type || stream_class->frozen) {
428 ret = -1;
429 goto end;
430 }
431
432 if (bt_ctf_field_type_get_type_id(event_context_type) !=
433 CTF_TYPE_STRUCT) {
434 /* A packet context must be a structure */
435 ret = -1;
436 goto end;
437 }
438
439 bt_ctf_field_type_put(stream_class->event_context_type);
440 bt_ctf_field_type_get(event_context_type);
441 stream_class->event_context_type = event_context_type;
442end:
443 return ret;
444}
445
11b0cdc8
JG
446void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
447{
448 if (!stream_class) {
449 return;
450 }
451
452 bt_ctf_ref_get(&stream_class->ref_count);
453}
454
455void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
456{
457 if (!stream_class) {
458 return;
459 }
460
461 bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy);
462}
463
464BT_HIDDEN
465void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
466{
c35a1669
JG
467 size_t i;
468
11b0cdc8
JG
469 if (!stream_class) {
470 return;
471 }
472
473 stream_class->frozen = 1;
662e778c 474 bt_ctf_field_type_freeze(stream_class->event_header_type);
12c8a1a3 475 bt_ctf_field_type_freeze(stream_class->packet_context_type);
af181248 476 bt_ctf_field_type_freeze(stream_class->event_context_type);
11b0cdc8 477 bt_ctf_clock_freeze(stream_class->clock);
c35a1669
JG
478
479 bt_ctf_field_type_set_native_byte_order(
480 stream_class->event_header_type, stream_class->byte_order);
481 bt_ctf_field_type_set_native_byte_order(
482 stream_class->packet_context_type, stream_class->byte_order);
483 bt_ctf_field_type_set_native_byte_order(
484 stream_class->event_context_type, stream_class->byte_order);
485 for (i = 0; i < stream_class->event_classes->len; i++) {
486 bt_ctf_event_class_set_native_byte_order(
487 g_ptr_array_index(stream_class->event_classes, i),
488 stream_class->byte_order);
489 bt_ctf_event_class_freeze(
490 g_ptr_array_index(stream_class->event_classes, i));
491 }
11b0cdc8
JG
492}
493
11b0cdc8
JG
494BT_HIDDEN
495int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
496 enum bt_ctf_byte_order byte_order)
497{
498 int ret = 0;
c35a1669 499 int internal_byte_order;
11b0cdc8 500
c35a1669
JG
501 /* Note that "NATIVE" means the trace's endianness, not the host's. */
502 if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN ||
503 byte_order > BT_CTF_BYTE_ORDER_NETWORK ||
504 stream_class->frozen) {
505 ret = -1;
506 goto end;
507 }
508
509 switch (byte_order) {
510 case BT_CTF_BYTE_ORDER_NETWORK:
511 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
512 internal_byte_order = BIG_ENDIAN;
513 break;
514 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
515 internal_byte_order = LITTLE_ENDIAN;
516 break;
517 default:
518 ret = -1;
11b0cdc8
JG
519 goto end;
520 }
c35a1669
JG
521
522 stream_class->byte_order = internal_byte_order;
11b0cdc8
JG
523end:
524 return ret;
525}
526
527BT_HIDDEN
528int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
529 struct metadata_context *context)
530{
2f100782 531 int64_t ret = 0;
11b0cdc8
JG
532 size_t i;
533
534 g_string_assign(context->field_name, "");
535 context->current_indentation_level = 1;
536 if (!stream_class->id_set) {
537 ret = -1;
538 goto end;
539 }
540
541 g_string_append_printf(context->string,
542 "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ",
543 stream_class->id);
544 ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
545 context);
546 if (ret) {
547 goto end;
548 }
549
550 g_string_append(context->string, ";\n\n\tpacket.context := ");
551 ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
552 context);
553 if (ret) {
554 goto end;
555 }
556
557 if (stream_class->event_context_type) {
558 g_string_append(context->string, ";\n\n\tevent.context := ");
559 ret = bt_ctf_field_type_serialize(
560 stream_class->event_context_type, context);
561 if (ret) {
562 goto end;
563 }
564 }
565
566 g_string_append(context->string, ";\n};\n\n");
11b0cdc8
JG
567 for (i = 0; i < stream_class->event_classes->len; i++) {
568 struct bt_ctf_event_class *event_class =
569 stream_class->event_classes->pdata[i];
570
11b0cdc8
JG
571 ret = bt_ctf_event_class_serialize(event_class, context);
572 if (ret) {
573 goto end;
574 }
575 }
576end:
577 context->current_indentation_level = 0;
578 return ret;
579}
580
581static
582void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
583{
584 struct bt_ctf_stream_class *stream_class;
585
586 if (!ref) {
587 return;
588 }
589
590 stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count);
591 bt_ctf_clock_put(stream_class->clock);
592
593 if (stream_class->event_classes) {
2f100782
JG
594 size_t i;
595
596 /* Unregister this stream class from the event classes */
597 for (i = 0; i < stream_class->event_classes->len; i++) {
598 struct bt_ctf_event_class *event_class =
599 g_ptr_array_index(stream_class->event_classes,
600 i);
601
602 bt_ctf_event_class_set_stream_class(event_class, NULL);
603 }
604
11b0cdc8
JG
605 g_ptr_array_free(stream_class->event_classes, TRUE);
606 }
607
608 if (stream_class->name) {
609 g_string_free(stream_class->name, TRUE);
610 }
611
612 bt_ctf_field_type_put(stream_class->event_header_type);
11b0cdc8 613 bt_ctf_field_type_put(stream_class->packet_context_type);
af181248
JG
614 if (stream_class->event_context_type) {
615 bt_ctf_field_type_put(stream_class->event_context_type);
616 }
11b0cdc8
JG
617 g_free(stream_class);
618}
619
620static
662e778c 621int init_event_header(struct bt_ctf_stream_class *stream_class)
11b0cdc8
JG
622{
623 int ret = 0;
624 struct bt_ctf_field_type *event_header_type =
625 bt_ctf_field_type_structure_create();
626 struct bt_ctf_field_type *_uint32_t =
627 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
628 struct bt_ctf_field_type *_uint64_t =
629 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
630
631 if (!event_header_type) {
632 ret = -1;
633 goto end;
634 }
635
11b0cdc8
JG
636 ret = bt_ctf_field_type_structure_add_field(event_header_type,
637 _uint32_t, "id");
638 if (ret) {
639 goto end;
640 }
641
642 ret = bt_ctf_field_type_structure_add_field(event_header_type,
643 _uint64_t, "timestamp");
644 if (ret) {
645 goto end;
646 }
647
662e778c
JG
648 if (stream_class->event_header_type) {
649 bt_ctf_field_type_put(stream_class->event_header_type);
de876b7f 650 }
662e778c 651 stream_class->event_header_type = event_header_type;
11b0cdc8
JG
652end:
653 if (ret) {
654 bt_ctf_field_type_put(event_header_type);
655 }
656
657 bt_ctf_field_type_put(_uint32_t);
658 bt_ctf_field_type_put(_uint64_t);
659 return ret;
660}
661
662static
662e778c 663int init_packet_context(struct bt_ctf_stream_class *stream_class)
11b0cdc8
JG
664{
665 int ret = 0;
666 struct bt_ctf_field_type *packet_context_type =
667 bt_ctf_field_type_structure_create();
668 struct bt_ctf_field_type *_uint64_t =
669 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
670
671 if (!packet_context_type) {
672 ret = -1;
673 goto end;
674 }
675
676 /*
677 * We create a stream packet context as proposed in the CTF
678 * specification.
679 */
11b0cdc8
JG
680 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
681 _uint64_t, "timestamp_begin");
682 if (ret) {
683 goto end;
684 }
685
686 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
687 _uint64_t, "timestamp_end");
688 if (ret) {
689 goto end;
690 }
691
692 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
693 _uint64_t, "content_size");
694 if (ret) {
695 goto end;
696 }
697
698 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
699 _uint64_t, "packet_size");
700 if (ret) {
701 goto end;
702 }
703
704 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
705 _uint64_t, "events_discarded");
706 if (ret) {
707 goto end;
708 }
709
662e778c
JG
710 if (stream_class->packet_context_type) {
711 bt_ctf_field_type_put(stream_class->packet_context_type);
712 }
11b0cdc8 713 stream_class->packet_context_type = packet_context_type;
11b0cdc8
JG
714end:
715 if (ret) {
716 bt_ctf_field_type_put(packet_context_type);
717 goto end;
718 }
719
720 bt_ctf_field_type_put(_uint64_t);
721 return ret;
722}
This page took 0.053221 seconds and 4 git commands to generate.