Add support for custom event headers
[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 || !strlen(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 struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
105 struct bt_ctf_stream_class *stream_class)
106 {
107 struct bt_ctf_clock *clock = NULL;
108
109 if (!stream_class || !stream_class->clock) {
110 goto end;
111 }
112
113 clock = stream_class->clock;
114 bt_ctf_clock_get(clock);
115 end:
116 return clock;
117 }
118
119 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
120 struct bt_ctf_clock *clock)
121 {
122 int ret = 0;
123
124 if (!stream_class || !clock || stream_class->frozen) {
125 ret = -1;
126 goto end;
127 }
128
129 if (stream_class->clock) {
130 bt_ctf_clock_put(stream_class->clock);
131 }
132
133 stream_class->clock = clock;
134 bt_ctf_clock_get(clock);
135 end:
136 return ret;
137 }
138
139 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class)
140 {
141 int64_t ret;
142
143 if (!stream_class || !stream_class->id_set) {
144 ret = -1;
145 goto end;
146 }
147
148 ret = (int64_t) stream_class->id;
149 end:
150 return ret;
151 }
152
153 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
154 uint32_t id)
155 {
156 int ret = 0;
157
158 if (!stream_class || stream_class->frozen) {
159 ret = -1;
160 goto end;
161 }
162
163 stream_class->id = id;
164 stream_class->id_set = 1;
165 end:
166 return ret;
167 }
168
169 int bt_ctf_stream_class_add_event_class(
170 struct bt_ctf_stream_class *stream_class,
171 struct bt_ctf_event_class *event_class)
172 {
173 int ret = 0;
174 int64_t event_id;
175
176 if (!stream_class || !event_class) {
177 ret = -1;
178 goto end;
179 }
180
181 /* Check for duplicate event classes */
182 struct search_query query = { .value = event_class, .found = 0 };
183 g_ptr_array_foreach(stream_class->event_classes, value_exists, &query);
184 if (query.found) {
185 ret = -1;
186 goto end;
187 }
188
189 /* Only set an event id if none was explicitly set before */
190 event_id = bt_ctf_event_class_get_id(event_class);
191 if (event_id < 0) {
192 if (bt_ctf_event_class_set_id(event_class,
193 stream_class->next_event_id++)) {
194 ret = -1;
195 goto end;
196 }
197 }
198
199 ret = bt_ctf_event_class_set_stream_class(event_class, stream_class);
200 if (ret) {
201 goto end;
202 }
203
204 bt_ctf_event_class_get(event_class);
205 g_ptr_array_add(stream_class->event_classes, event_class);
206 end:
207 return ret;
208 }
209
210 int bt_ctf_stream_class_get_event_class_count(
211 struct bt_ctf_stream_class *stream_class)
212 {
213 int ret;
214
215 if (!stream_class) {
216 ret = -1;
217 goto end;
218 }
219
220 ret = (int) stream_class->event_classes->len;
221 end:
222 return ret;
223 }
224
225 struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
226 struct bt_ctf_stream_class *stream_class, int index)
227 {
228 struct bt_ctf_event_class *event_class = NULL;
229
230 if (!stream_class || index < 0 ||
231 index >= stream_class->event_classes->len) {
232 goto end;
233 }
234
235 event_class = g_ptr_array_index(stream_class->event_classes, index);
236 bt_ctf_event_class_get(event_class);
237 end:
238 return event_class;
239 }
240
241 struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
242 struct bt_ctf_stream_class *stream_class, const char *name)
243 {
244 size_t i;
245 GQuark name_quark;
246 struct bt_ctf_event_class *event_class = NULL;
247
248 if (!stream_class || !name) {
249 goto end;
250 }
251
252 name_quark = g_quark_try_string(name);
253 if (!name_quark) {
254 goto end;
255 }
256
257 for (i = 0; i < stream_class->event_classes->len; i++) {
258 struct bt_ctf_event_class *current_event_class =
259 g_ptr_array_index(stream_class->event_classes, i);
260
261 if (name_quark == current_event_class->name) {
262 event_class = current_event_class;
263 bt_ctf_event_class_get(event_class);
264 goto end;
265 }
266 }
267 end:
268 return event_class;
269 }
270
271 struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
272 struct bt_ctf_stream_class *stream_class)
273 {
274 struct bt_ctf_field_type *ret = NULL;
275
276 if (!stream_class) {
277 goto end;
278 }
279
280 assert(stream_class->packet_context_type);
281 bt_ctf_field_type_get(stream_class->packet_context_type);
282 ret = stream_class->packet_context_type;
283 end:
284 return ret;
285 }
286
287 int bt_ctf_stream_class_set_packet_context_type(
288 struct bt_ctf_stream_class *stream_class,
289 struct bt_ctf_field_type *packet_context_type)
290 {
291 int ret = 0;
292
293 if (!stream_class || !packet_context_type || stream_class->frozen) {
294 ret = -1;
295 goto end;
296 }
297
298 assert(stream_class->packet_context_type);
299 if (stream_class->packet_context_type == packet_context_type) {
300 goto end;
301 }
302 if (bt_ctf_field_type_get_type_id(packet_context_type) !=
303 CTF_TYPE_STRUCT) {
304 /* A packet context must be a structure */
305 ret = -1;
306 goto end;
307 }
308
309 bt_ctf_field_type_put(stream_class->packet_context_type);
310 bt_ctf_field_type_get(packet_context_type);
311 stream_class->packet_context_type = packet_context_type;
312 end:
313 return ret;
314 }
315
316 struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type(
317 struct bt_ctf_stream_class *stream_class)
318 {
319 struct bt_ctf_field_type *ret = NULL;
320
321 if (!stream_class || !stream_class->event_header_type) {
322 goto end;
323 }
324
325 assert(stream_class->event_header_type);
326 bt_ctf_field_type_get(stream_class->event_header_type);
327 ret = stream_class->event_header_type;
328 end:
329 return ret;
330 }
331
332 int bt_ctf_stream_class_set_event_header_type(
333 struct bt_ctf_stream_class *stream_class,
334 struct bt_ctf_field_type *event_header_type)
335 {
336 int ret = 0;
337
338 if (!stream_class || !event_header_type || stream_class->frozen) {
339 ret = -1;
340 goto end;
341 }
342
343 assert(stream_class->event_header_type);
344 if (stream_class->event_header_type == event_header_type) {
345 goto end;
346 }
347 if (bt_ctf_field_type_get_type_id(event_header_type) !=
348 CTF_TYPE_STRUCT) {
349 /* An event header must be a structure */
350 ret = -1;
351 goto end;
352 }
353
354 bt_ctf_field_type_put(stream_class->event_header_type);
355 bt_ctf_field_type_get(event_header_type);
356 stream_class->event_header_type = event_header_type;
357 end:
358 return ret;
359 }
360
361 struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
362 struct bt_ctf_stream_class *stream_class)
363 {
364 struct bt_ctf_field_type *ret = NULL;
365
366 if (!stream_class || !stream_class->event_context_type) {
367 goto end;
368 }
369
370 assert(stream_class->event_context_type);
371 bt_ctf_field_type_get(stream_class->event_context_type);
372 ret = stream_class->event_context_type;
373 end:
374 return ret;
375 }
376
377 int bt_ctf_stream_class_set_event_context_type(
378 struct bt_ctf_stream_class *stream_class,
379 struct bt_ctf_field_type *event_context_type)
380 {
381 int ret = 0;
382
383 if (!stream_class || !event_context_type || stream_class->frozen) {
384 ret = -1;
385 goto end;
386 }
387
388 if (bt_ctf_field_type_get_type_id(event_context_type) !=
389 CTF_TYPE_STRUCT) {
390 /* A packet context must be a structure */
391 ret = -1;
392 goto end;
393 }
394
395 bt_ctf_field_type_put(stream_class->event_context_type);
396 bt_ctf_field_type_get(event_context_type);
397 stream_class->event_context_type = event_context_type;
398 end:
399 return ret;
400 }
401
402 void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
403 {
404 if (!stream_class) {
405 return;
406 }
407
408 bt_ctf_ref_get(&stream_class->ref_count);
409 }
410
411 void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
412 {
413 if (!stream_class) {
414 return;
415 }
416
417 bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy);
418 }
419
420 BT_HIDDEN
421 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
422 {
423 size_t i;
424
425 if (!stream_class) {
426 return;
427 }
428
429 stream_class->frozen = 1;
430 bt_ctf_field_type_freeze(stream_class->event_header_type);
431 bt_ctf_field_type_freeze(stream_class->packet_context_type);
432 bt_ctf_field_type_freeze(stream_class->event_context_type);
433 bt_ctf_clock_freeze(stream_class->clock);
434
435 bt_ctf_field_type_set_native_byte_order(
436 stream_class->event_header_type, stream_class->byte_order);
437 bt_ctf_field_type_set_native_byte_order(
438 stream_class->packet_context_type, stream_class->byte_order);
439 bt_ctf_field_type_set_native_byte_order(
440 stream_class->event_context_type, stream_class->byte_order);
441 for (i = 0; i < stream_class->event_classes->len; i++) {
442 bt_ctf_event_class_set_native_byte_order(
443 g_ptr_array_index(stream_class->event_classes, i),
444 stream_class->byte_order);
445 bt_ctf_event_class_freeze(
446 g_ptr_array_index(stream_class->event_classes, i));
447 }
448 }
449
450 BT_HIDDEN
451 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
452 enum bt_ctf_byte_order byte_order)
453 {
454 int ret = 0;
455 int internal_byte_order;
456
457 /* Note that "NATIVE" means the trace's endianness, not the host's. */
458 if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN ||
459 byte_order > BT_CTF_BYTE_ORDER_NETWORK ||
460 stream_class->frozen) {
461 ret = -1;
462 goto end;
463 }
464
465 switch (byte_order) {
466 case BT_CTF_BYTE_ORDER_NETWORK:
467 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
468 internal_byte_order = BIG_ENDIAN;
469 break;
470 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
471 internal_byte_order = LITTLE_ENDIAN;
472 break;
473 default:
474 ret = -1;
475 goto end;
476 }
477
478 stream_class->byte_order = internal_byte_order;
479 end:
480 return ret;
481 }
482
483 BT_HIDDEN
484 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
485 struct metadata_context *context)
486 {
487 int64_t ret = 0;
488 size_t i;
489
490 g_string_assign(context->field_name, "");
491 context->current_indentation_level = 1;
492 if (!stream_class->id_set) {
493 ret = -1;
494 goto end;
495 }
496
497 g_string_append_printf(context->string,
498 "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ",
499 stream_class->id);
500 ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
501 context);
502 if (ret) {
503 goto end;
504 }
505
506 g_string_append(context->string, ";\n\n\tpacket.context := ");
507 ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
508 context);
509 if (ret) {
510 goto end;
511 }
512
513 if (stream_class->event_context_type) {
514 g_string_append(context->string, ";\n\n\tevent.context := ");
515 ret = bt_ctf_field_type_serialize(
516 stream_class->event_context_type, context);
517 if (ret) {
518 goto end;
519 }
520 }
521
522 g_string_append(context->string, ";\n};\n\n");
523 for (i = 0; i < stream_class->event_classes->len; i++) {
524 struct bt_ctf_event_class *event_class =
525 stream_class->event_classes->pdata[i];
526
527 ret = bt_ctf_event_class_serialize(event_class, context);
528 if (ret) {
529 goto end;
530 }
531 }
532 end:
533 context->current_indentation_level = 0;
534 return ret;
535 }
536
537 static
538 void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
539 {
540 struct bt_ctf_stream_class *stream_class;
541
542 if (!ref) {
543 return;
544 }
545
546 stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count);
547 bt_ctf_clock_put(stream_class->clock);
548
549 if (stream_class->event_classes) {
550 size_t i;
551
552 /* Unregister this stream class from the event classes */
553 for (i = 0; i < stream_class->event_classes->len; i++) {
554 struct bt_ctf_event_class *event_class =
555 g_ptr_array_index(stream_class->event_classes,
556 i);
557
558 bt_ctf_event_class_set_stream_class(event_class, NULL);
559 }
560
561 g_ptr_array_free(stream_class->event_classes, TRUE);
562 }
563
564 if (stream_class->name) {
565 g_string_free(stream_class->name, TRUE);
566 }
567
568 bt_ctf_field_type_put(stream_class->event_header_type);
569 bt_ctf_field_type_put(stream_class->packet_context_type);
570 if (stream_class->event_context_type) {
571 bt_ctf_field_type_put(stream_class->event_context_type);
572 }
573 g_free(stream_class);
574 }
575
576 static
577 int init_event_header(struct bt_ctf_stream_class *stream_class)
578 {
579 int ret = 0;
580 struct bt_ctf_field_type *event_header_type =
581 bt_ctf_field_type_structure_create();
582 struct bt_ctf_field_type *_uint32_t =
583 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
584 struct bt_ctf_field_type *_uint64_t =
585 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
586
587 if (!event_header_type) {
588 ret = -1;
589 goto end;
590 }
591
592 ret = bt_ctf_field_type_structure_add_field(event_header_type,
593 _uint32_t, "id");
594 if (ret) {
595 goto end;
596 }
597
598 ret = bt_ctf_field_type_structure_add_field(event_header_type,
599 _uint64_t, "timestamp");
600 if (ret) {
601 goto end;
602 }
603
604 if (stream_class->event_header_type) {
605 bt_ctf_field_type_put(stream_class->event_header_type);
606 }
607 stream_class->event_header_type = event_header_type;
608 end:
609 if (ret) {
610 bt_ctf_field_type_put(event_header_type);
611 }
612
613 bt_ctf_field_type_put(_uint32_t);
614 bt_ctf_field_type_put(_uint64_t);
615 return ret;
616 }
617
618 static
619 int init_packet_context(struct bt_ctf_stream_class *stream_class)
620 {
621 int ret = 0;
622 struct bt_ctf_field_type *packet_context_type =
623 bt_ctf_field_type_structure_create();
624 struct bt_ctf_field_type *_uint64_t =
625 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
626
627 if (!packet_context_type) {
628 ret = -1;
629 goto end;
630 }
631
632 /*
633 * We create a stream packet context as proposed in the CTF
634 * specification.
635 */
636 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
637 _uint64_t, "timestamp_begin");
638 if (ret) {
639 goto end;
640 }
641
642 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
643 _uint64_t, "timestamp_end");
644 if (ret) {
645 goto end;
646 }
647
648 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
649 _uint64_t, "content_size");
650 if (ret) {
651 goto end;
652 }
653
654 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
655 _uint64_t, "packet_size");
656 if (ret) {
657 goto end;
658 }
659
660 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
661 _uint64_t, "events_discarded");
662 if (ret) {
663 goto end;
664 }
665
666 if (stream_class->packet_context_type) {
667 bt_ctf_field_type_put(stream_class->packet_context_type);
668 }
669 stream_class->packet_context_type = packet_context_type;
670 end:
671 if (ret) {
672 bt_ctf_field_type_put(packet_context_type);
673 goto end;
674 }
675
676 bt_ctf_field_type_put(_uint64_t);
677 return ret;
678 }
This page took 0.042761 seconds and 4 git commands to generate.