Fix: Check stream fd value before closing
[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 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
197 uint32_t id)
198 {
199 int ret = 0;
200
201 if (!stream_class || stream_class->frozen) {
202 ret = -1;
203 goto end;
204 }
205
206 stream_class->id = id;
207 stream_class->id_set = 1;
208 end:
209 return ret;
210 }
211
212 int 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;
217 int64_t event_id;
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
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) {
244 goto end;
245 }
246
247 bt_ctf_event_class_get(event_class);
248 g_ptr_array_add(stream_class->event_classes, event_class);
249 bt_ctf_event_class_freeze(event_class);
250 end:
251 return ret;
252 }
253
254 int bt_ctf_stream_class_get_event_class_count(
255 struct bt_ctf_stream_class *stream_class)
256 {
257 int ret;
258
259 if (!stream_class) {
260 ret = -1;
261 goto end;
262 }
263
264 ret = (int) stream_class->event_classes->len;
265 end:
266 return ret;
267 }
268
269 struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
270 struct bt_ctf_stream_class *stream_class, int index)
271 {
272 struct bt_ctf_event_class *event_class = NULL;
273
274 if (!stream_class || index < 0 ||
275 index >= stream_class->event_classes->len) {
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);
281 end:
282 return event_class;
283 }
284
285 struct 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 }
311 end:
312 return event_class;
313 }
314
315 struct 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;
327 end:
328 return ret;
329 }
330
331 int 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
337 if (!stream_class || !packet_context_type || stream_class->frozen) {
338 ret = -1;
339 goto end;
340 }
341
342 assert(stream_class->packet_context_type);
343 if (stream_class->packet_context_type == packet_context_type) {
344 goto end;
345 }
346 if (bt_ctf_field_type_get_type_id(packet_context_type) !=
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;
356 end:
357 return ret;
358 }
359
360 struct 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;
372 end:
373 return ret;
374 }
375
376 int 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;
401 end:
402 return ret;
403 }
404
405 struct 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;
417 end:
418 return ret;
419 }
420
421 int 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;
442 end:
443 return ret;
444 }
445
446 void 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
455 void 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
464 BT_HIDDEN
465 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
466 {
467 size_t i;
468
469 if (!stream_class) {
470 return;
471 }
472
473 stream_class->frozen = 1;
474 bt_ctf_field_type_freeze(stream_class->event_header_type);
475 bt_ctf_field_type_freeze(stream_class->packet_context_type);
476 bt_ctf_field_type_freeze(stream_class->event_context_type);
477 bt_ctf_clock_freeze(stream_class->clock);
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 }
492 }
493
494 BT_HIDDEN
495 int 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;
499 int internal_byte_order;
500
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;
519 goto end;
520 }
521
522 stream_class->byte_order = internal_byte_order;
523 end:
524 return ret;
525 }
526
527 BT_HIDDEN
528 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
529 struct metadata_context *context)
530 {
531 int64_t ret = 0;
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");
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
571 ret = bt_ctf_event_class_serialize(event_class, context);
572 if (ret) {
573 goto end;
574 }
575 }
576 end:
577 context->current_indentation_level = 0;
578 return ret;
579 }
580
581 static
582 void 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) {
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
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);
613 bt_ctf_field_type_put(stream_class->packet_context_type);
614 if (stream_class->event_context_type) {
615 bt_ctf_field_type_put(stream_class->event_context_type);
616 }
617 g_free(stream_class);
618 }
619
620 static
621 int init_event_header(struct bt_ctf_stream_class *stream_class)
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
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
648 if (stream_class->event_header_type) {
649 bt_ctf_field_type_put(stream_class->event_header_type);
650 }
651 stream_class->event_header_type = event_header_type;
652 end:
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
662 static
663 int init_packet_context(struct bt_ctf_stream_class *stream_class)
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 */
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
710 if (stream_class->packet_context_type) {
711 bt_ctf_field_type_put(stream_class->packet_context_type);
712 }
713 stream_class->packet_context_type = packet_context_type;
714 end:
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.045119 seconds and 5 git commands to generate.