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