ir: rename event-fields/event-types -> fields/field-types
[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>
2e33ac5a
PP
33#include <babeltrace/ctf-ir/field-types-internal.h>
34#include <babeltrace/ctf-ir/fields-internal.h>
11b0cdc8
JG
35#include <babeltrace/ctf-writer/stream.h>
36#include <babeltrace/ctf-ir/stream-class-internal.h>
09840de5 37#include <babeltrace/ctf-ir/validation-internal.h>
11b0cdc8 38#include <babeltrace/ctf-writer/functor-internal.h>
654c1444 39#include <babeltrace/ctf-ir/utils.h>
83509119 40#include <babeltrace/ref.h>
11b0cdc8
JG
41#include <babeltrace/compiler.h>
42#include <babeltrace/align.h>
a0b720b2 43#include <babeltrace/endian.h>
11b0cdc8
JG
44
45static
83509119 46void bt_ctf_stream_class_destroy(struct bt_object *obj);
11b0cdc8 47static
662e778c 48int init_event_header(struct bt_ctf_stream_class *stream_class);
11b0cdc8 49static
662e778c 50int init_packet_context(struct bt_ctf_stream_class *stream_class);
11b0cdc8
JG
51
52struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
53{
12c8a1a3 54 int ret;
11b0cdc8
JG
55 struct bt_ctf_stream_class *stream_class = NULL;
56
3ea33115 57 if (name && bt_ctf_validate_identifier(name)) {
11b0cdc8
JG
58 goto error;
59 }
60
61 stream_class = g_new0(struct bt_ctf_stream_class, 1);
62 if (!stream_class) {
63 goto error;
64 }
65
66 stream_class->name = g_string_new(name);
67 stream_class->event_classes = g_ptr_array_new_with_free_func(
e6a8e8e4 68 (GDestroyNotify) bt_object_release);
11b0cdc8 69 if (!stream_class->event_classes) {
83509119 70 goto error;
11b0cdc8
JG
71 }
72
662e778c
JG
73 ret = init_event_header(stream_class);
74 if (ret) {
83509119 75 goto error;
662e778c
JG
76 }
77
78 ret = init_packet_context(stream_class);
12c8a1a3 79 if (ret) {
83509119 80 goto error;
12c8a1a3
JG
81 }
82
83509119 83 bt_object_init(stream_class, bt_ctf_stream_class_destroy);
11b0cdc8
JG
84 return stream_class;
85
11b0cdc8 86error:
83509119 87 BT_PUT(stream_class);
11b0cdc8
JG
88 return stream_class;
89}
90
142c5610
JG
91struct bt_ctf_trace *bt_ctf_stream_class_get_trace(
92 struct bt_ctf_stream_class *stream_class)
93{
e6a8e8e4
JG
94 return (struct bt_ctf_trace *) bt_object_get_parent(
95 stream_class);
142c5610
JG
96}
97
69dc4535
JG
98const char *bt_ctf_stream_class_get_name(
99 struct bt_ctf_stream_class *stream_class)
100{
101 const char *name = NULL;
102
103 if (!stream_class) {
104 goto end;
105 }
106
107 name = stream_class->name->str;
108end:
109 return name;
110}
111
3ea33115
JG
112int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class,
113 const char *name)
114{
115 int ret = 0;
116
117 if (!stream_class || stream_class->frozen) {
118 ret = -1;
119 goto end;
120 }
121
122 g_string_assign(stream_class->name, name);
123end:
124 return ret;
125}
126
2f100782
JG
127struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
128 struct bt_ctf_stream_class *stream_class)
129{
130 struct bt_ctf_clock *clock = NULL;
131
132 if (!stream_class || !stream_class->clock) {
133 goto end;
134 }
135
136 clock = stream_class->clock;
83509119 137 bt_get(clock);
2f100782
JG
138end:
139 return clock;
140}
141
11b0cdc8
JG
142int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
143 struct bt_ctf_clock *clock)
144{
145 int ret = 0;
eee752e5 146 struct bt_ctf_field_type *timestamp_field = NULL;
11b0cdc8
JG
147
148 if (!stream_class || !clock || stream_class->frozen) {
149 ret = -1;
150 goto end;
151 }
152
eee752e5
JG
153 /*
154 * Look for a "timestamp" field in the stream class' event header type
155 * and map the stream's clock to that field if no current mapping is
156 * currently set.
157 */
158 timestamp_field = bt_ctf_field_type_structure_get_field_type_by_name(
159 stream_class->event_header_type, "timestamp");
160 if (timestamp_field) {
161 struct bt_ctf_clock *mapped_clock;
162
163 mapped_clock = bt_ctf_field_type_integer_get_mapped_clock(
164 timestamp_field);
165 if (mapped_clock) {
83509119 166 bt_put(mapped_clock);
eee752e5
JG
167 goto end;
168 }
169
170 ret = bt_ctf_field_type_integer_set_mapped_clock(
171 timestamp_field, clock);
172 if (ret) {
173 goto end;
174 }
175 }
176
11b0cdc8 177 if (stream_class->clock) {
83509119 178 bt_put(stream_class->clock);
11b0cdc8
JG
179 }
180
181 stream_class->clock = clock;
83509119 182 bt_get(clock);
11b0cdc8 183end:
eee752e5 184 if (timestamp_field) {
83509119 185 bt_put(timestamp_field);
eee752e5 186 }
11b0cdc8
JG
187 return ret;
188}
189
2f100782
JG
190int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class)
191{
192 int64_t ret;
193
194 if (!stream_class || !stream_class->id_set) {
195 ret = -1;
196 goto end;
197 }
198
199 ret = (int64_t) stream_class->id;
200end:
201 return ret;
202}
203
5ca83563
JG
204BT_HIDDEN
205int _bt_ctf_stream_class_set_id(
206 struct bt_ctf_stream_class *stream_class, uint32_t id)
207{
208 stream_class->id = id;
209 stream_class->id_set = 1;
210 return 0;
211}
212
29664b2a
PP
213struct event_class_set_stream_id_data {
214 uint32_t stream_id;
215 int ret;
216};
217
218static
219void event_class_set_stream_id(gpointer event_class, gpointer data)
220{
221 struct event_class_set_stream_id_data *typed_data = data;
222
223 typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class,
224 typed_data->stream_id);
225}
226
227BT_HIDDEN
228int bt_ctf_stream_class_set_id_no_check(
229 struct bt_ctf_stream_class *stream_class, uint32_t id)
2f100782
JG
230{
231 int ret = 0;
29664b2a
PP
232 struct event_class_set_stream_id_data data =
233 { .stream_id = id, .ret = 0 };
2f100782 234
29664b2a
PP
235 /*
236 * Make sure all event classes have their "stream_id" attribute
237 * set to this value.
238 */
239 g_ptr_array_foreach(stream_class->event_classes,
240 event_class_set_stream_id, &data);
241 ret = data.ret;
242 if (ret) {
2f100782
JG
243 goto end;
244 }
245
5ca83563
JG
246 ret = _bt_ctf_stream_class_set_id(stream_class, id);
247 if (ret) {
248 goto end;
249 }
2f100782
JG
250end:
251 return ret;
252}
253
29664b2a
PP
254int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
255 uint32_t id)
256{
257 int ret = 0;
258
259 if (!stream_class || stream_class->frozen) {
260 ret = -1;
261 goto end;
262 }
263
264 ret = bt_ctf_stream_class_set_id_no_check(stream_class, id);
265end:
266 return ret;
267}
268
0d23acbe
PP
269static
270void event_class_exists(gpointer element, gpointer query)
271{
272 struct bt_ctf_event_class *event_class_a = element;
273 struct search_query *search_query = query;
274 struct bt_ctf_event_class *event_class_b = search_query->value;
275 int64_t id_a, id_b;
276
277 if (search_query->value == element) {
278 search_query->found = 1;
279 goto end;
280 }
281
282 /*
283 * Two event classes cannot share the same name in a given
284 * stream class.
285 */
286 if (!strcmp(bt_ctf_event_class_get_name(event_class_a),
287 bt_ctf_event_class_get_name(event_class_b))) {
288 search_query->found = 1;
289 goto end;
290 }
291
292 /*
293 * Two event classes cannot share the same ID in a given
294 * stream class.
295 */
296 id_a = bt_ctf_event_class_get_id(event_class_a);
297 id_b = bt_ctf_event_class_get_id(event_class_b);
298
299 if (id_a < 0 || id_b < 0) {
300 /* at least one ID is not set: will be automatically set later */
301 goto end;
302 }
303
304 if (id_a == id_b) {
305 search_query->found = 1;
306 goto end;
307 }
308
309end:
310 return;
311}
312
11b0cdc8
JG
313int bt_ctf_stream_class_add_event_class(
314 struct bt_ctf_stream_class *stream_class,
315 struct bt_ctf_event_class *event_class)
316{
317 int ret = 0;
2f100782 318 int64_t event_id;
e6a8e8e4
JG
319 struct bt_ctf_trace *trace = NULL;
320 struct bt_ctf_stream_class *old_stream_class = NULL;
09840de5
PP
321 struct bt_ctf_validation_output validation_output = { 0 };
322 struct bt_ctf_field_type *packet_header_type = NULL;
323 struct bt_ctf_field_type *packet_context_type = NULL;
324 struct bt_ctf_field_type *event_header_type = NULL;
325 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
326 struct bt_ctf_field_type *event_context_type = NULL;
327 struct bt_ctf_field_type *event_payload_type = NULL;
328 const enum bt_ctf_validation_flag validation_flags =
329 BT_CTF_VALIDATION_FLAG_EVENT;
11b0cdc8
JG
330
331 if (!stream_class || !event_class) {
332 ret = -1;
333 goto end;
334 }
335
336 /* Check for duplicate event classes */
337 struct search_query query = { .value = event_class, .found = 0 };
0d23acbe
PP
338 g_ptr_array_foreach(stream_class->event_classes, event_class_exists,
339 &query);
11b0cdc8
JG
340 if (query.found) {
341 ret = -1;
342 goto end;
343 }
344
09840de5 345 old_stream_class = bt_ctf_event_class_get_stream_class(event_class);
e6a8e8e4
JG
346 if (old_stream_class) {
347 /* Event class is already associated to a stream class. */
348 ret = -1;
349 goto end;
350 }
351
09840de5 352 trace = bt_ctf_stream_class_get_trace(stream_class);
e6a8e8e4 353 if (trace) {
09840de5
PP
354 /*
355 * If the stream class is associated with a trace, then
356 * both those objects are frozen. Also, this event class
357 * is about to be frozen.
358 *
359 * Therefore the event class must be validated here.
360 * The trace and stream class should be valid at this
361 * point.
362 */
363 assert(trace->valid);
364 assert(stream_class->valid);
365 packet_header_type =
366 bt_ctf_trace_get_packet_header_type(trace);
367 packet_context_type =
368 bt_ctf_stream_class_get_packet_context_type(
369 stream_class);
370 event_header_type =
371 bt_ctf_stream_class_get_event_header_type(stream_class);
372 stream_event_ctx_type =
373 bt_ctf_stream_class_get_event_context_type(
374 stream_class);
375 event_context_type =
376 bt_ctf_event_class_get_context_type(event_class);
377 event_payload_type =
378 bt_ctf_event_class_get_payload_type(event_class);
379 ret = bt_ctf_validate_class_types(
380 trace->environment, packet_header_type,
381 packet_context_type, event_header_type,
382 stream_event_ctx_type, event_context_type,
383 event_payload_type, trace->valid,
384 stream_class->valid, event_class->valid,
385 &validation_output, validation_flags);
386 BT_PUT(packet_header_type);
387 BT_PUT(packet_context_type);
388 BT_PUT(event_header_type);
389 BT_PUT(stream_event_ctx_type);
390 BT_PUT(event_context_type);
391 BT_PUT(event_payload_type);
392
26079216 393 if (ret) {
09840de5
PP
394 /*
395 * This means something went wrong during the
396 * validation process, not that the objects are
397 * invalid.
398 */
399 goto end;
400 }
401
402 if ((validation_output.valid_flags & validation_flags) !=
403 validation_flags) {
404 /* Invalid event class */
405 ret = -1;
26079216
JG
406 goto end;
407 }
408 }
409
09840de5 410 /* Only set an event ID if none was explicitly set before */
2f100782
JG
411 event_id = bt_ctf_event_class_get_id(event_class);
412 if (event_id < 0) {
413 if (bt_ctf_event_class_set_id(event_class,
414 stream_class->next_event_id++)) {
415 ret = -1;
416 goto end;
417 }
418 }
419
29664b2a
PP
420 ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id);
421 if (ret) {
422 goto end;
423 }
424
e6a8e8e4 425 bt_object_set_parent(event_class, stream_class);
09840de5
PP
426
427 if (trace) {
428 /*
429 * At this point we know that the function will be
430 * successful. Therefore we can replace the event
431 * class's field types with what's in the validation
432 * output structure and mark this event class as valid.
433 */
434 bt_ctf_validation_replace_types(NULL, NULL, event_class,
435 &validation_output, validation_flags);
436 event_class->valid = 1;
437
438 /*
439 * Put what was not moved in
440 * bt_ctf_validation_replace_types().
441 */
442 bt_ctf_validation_output_put_types(&validation_output);
443 }
444
445 /* Add to the event classes of the stream class */
11b0cdc8 446 g_ptr_array_add(stream_class->event_classes, event_class);
09840de5
PP
447
448 /* Freeze the event class */
58203827 449 bt_ctf_event_class_freeze(event_class);
5ca83563
JG
450
451 if (stream_class->byte_order) {
452 /*
453 * Only set native byte order if it has been initialized
454 * when the stream class was added to a trace.
455 *
456 * If not set here, this will be set when the stream
09840de5 457 * class is added to a trace.
5ca83563
JG
458 */
459 bt_ctf_event_class_set_native_byte_order(event_class,
460 stream_class->byte_order);
461 }
09840de5 462
11b0cdc8 463end:
e6a8e8e4
JG
464 BT_PUT(trace);
465 BT_PUT(old_stream_class);
09840de5
PP
466 bt_ctf_validation_output_put_types(&validation_output);
467 assert(!packet_header_type);
468 assert(!packet_context_type);
469 assert(!event_header_type);
470 assert(!stream_event_ctx_type);
471 assert(!event_context_type);
472 assert(!event_payload_type);
473
11b0cdc8
JG
474 return ret;
475}
476
074ee56d 477int bt_ctf_stream_class_get_event_class_count(
69dc4535
JG
478 struct bt_ctf_stream_class *stream_class)
479{
074ee56d 480 int ret;
69dc4535
JG
481
482 if (!stream_class) {
483 ret = -1;
484 goto end;
485 }
486
074ee56d 487 ret = (int) stream_class->event_classes->len;
69dc4535
JG
488end:
489 return ret;
490}
491
492struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
074ee56d 493 struct bt_ctf_stream_class *stream_class, int index)
69dc4535
JG
494{
495 struct bt_ctf_event_class *event_class = NULL;
496
074ee56d
JG
497 if (!stream_class || index < 0 ||
498 index >= stream_class->event_classes->len) {
69dc4535
JG
499 goto end;
500 }
501
502 event_class = g_ptr_array_index(stream_class->event_classes, index);
83509119 503 bt_get(event_class);
69dc4535
JG
504end:
505 return event_class;
506}
507
508struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
509 struct bt_ctf_stream_class *stream_class, const char *name)
510{
511 size_t i;
69dc4535
JG
512 struct bt_ctf_event_class *event_class = NULL;
513
514 if (!stream_class || !name) {
515 goto end;
516 }
517
69dc4535 518 for (i = 0; i < stream_class->event_classes->len; i++) {
b8248cc0 519 struct bt_ctf_event_class *cur_event_class =
69dc4535 520 g_ptr_array_index(stream_class->event_classes, i);
b8248cc0
PP
521 const char *cur_event_class_name =
522 bt_ctf_event_class_get_name(cur_event_class);
69dc4535 523
b8248cc0
PP
524 if (!strcmp(name, cur_event_class_name)) {
525 event_class = cur_event_class;
83509119 526 bt_get(event_class);
69dc4535
JG
527 goto end;
528 }
529 }
530end:
531 return event_class;
532}
533
0863f950
PP
534struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id(
535 struct bt_ctf_stream_class *stream_class, uint32_t id)
536{
537 size_t i;
538 struct bt_ctf_event_class *event_class = NULL;
539
540 if (!stream_class) {
541 goto end;
542 }
543
544 for (i = 0; i < stream_class->event_classes->len; i++) {
545 struct bt_ctf_event_class *current_event_class =
546 g_ptr_array_index(stream_class->event_classes, i);
547
548 if (bt_ctf_event_class_get_id(current_event_class) == id) {
549 event_class = current_event_class;
83509119 550 bt_get(event_class);
0863f950
PP
551 goto end;
552 }
553 }
554end:
555 return event_class;
556}
557
12c8a1a3
JG
558struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
559 struct bt_ctf_stream_class *stream_class)
560{
561 struct bt_ctf_field_type *ret = NULL;
562
563 if (!stream_class) {
564 goto end;
565 }
566
567 assert(stream_class->packet_context_type);
83509119 568 bt_get(stream_class->packet_context_type);
12c8a1a3
JG
569 ret = stream_class->packet_context_type;
570end:
571 return ret;
572}
573
574int bt_ctf_stream_class_set_packet_context_type(
575 struct bt_ctf_stream_class *stream_class,
576 struct bt_ctf_field_type *packet_context_type)
577{
578 int ret = 0;
579
0a00bfa1 580 if (!stream_class || !packet_context_type || stream_class->frozen) {
12c8a1a3
JG
581 ret = -1;
582 goto end;
583 }
584
585 assert(stream_class->packet_context_type);
662e778c
JG
586 if (stream_class->packet_context_type == packet_context_type) {
587 goto end;
588 }
b34f4d90 589 if (bt_ctf_field_type_get_type_id(packet_context_type) !=
9a19a512 590 BT_CTF_TYPE_ID_STRUCT) {
12c8a1a3
JG
591 /* A packet context must be a structure */
592 ret = -1;
593 goto end;
594 }
595
83509119
JG
596 bt_put(stream_class->packet_context_type);
597 bt_get(packet_context_type);
12c8a1a3
JG
598 stream_class->packet_context_type = packet_context_type;
599end:
600 return ret;
601}
602
662e778c
JG
603struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type(
604 struct bt_ctf_stream_class *stream_class)
605{
606 struct bt_ctf_field_type *ret = NULL;
607
608 if (!stream_class || !stream_class->event_header_type) {
609 goto end;
610 }
611
612 assert(stream_class->event_header_type);
83509119 613 bt_get(stream_class->event_header_type);
662e778c
JG
614 ret = stream_class->event_header_type;
615end:
616 return ret;
617}
618
619int bt_ctf_stream_class_set_event_header_type(
620 struct bt_ctf_stream_class *stream_class,
621 struct bt_ctf_field_type *event_header_type)
622{
623 int ret = 0;
624
625 if (!stream_class || !event_header_type || stream_class->frozen) {
626 ret = -1;
627 goto end;
628 }
629
630 assert(stream_class->event_header_type);
631 if (stream_class->event_header_type == event_header_type) {
632 goto end;
633 }
634 if (bt_ctf_field_type_get_type_id(event_header_type) !=
9a19a512 635 BT_CTF_TYPE_ID_STRUCT) {
662e778c
JG
636 /* An event header must be a structure */
637 ret = -1;
638 goto end;
639 }
640
83509119
JG
641 bt_put(stream_class->event_header_type);
642 bt_get(event_header_type);
662e778c
JG
643 stream_class->event_header_type = event_header_type;
644end:
645 return ret;
646}
647
af181248
JG
648struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
649 struct bt_ctf_stream_class *stream_class)
650{
651 struct bt_ctf_field_type *ret = NULL;
652
653 if (!stream_class || !stream_class->event_context_type) {
654 goto end;
655 }
656
657 assert(stream_class->event_context_type);
83509119 658 bt_get(stream_class->event_context_type);
af181248
JG
659 ret = stream_class->event_context_type;
660end:
661 return ret;
662}
663
664int bt_ctf_stream_class_set_event_context_type(
665 struct bt_ctf_stream_class *stream_class,
666 struct bt_ctf_field_type *event_context_type)
667{
668 int ret = 0;
669
670 if (!stream_class || !event_context_type || stream_class->frozen) {
671 ret = -1;
672 goto end;
673 }
674
675 if (bt_ctf_field_type_get_type_id(event_context_type) !=
9a19a512 676 BT_CTF_TYPE_ID_STRUCT) {
af181248
JG
677 /* A packet context must be a structure */
678 ret = -1;
679 goto end;
680 }
681
83509119
JG
682 bt_put(stream_class->event_context_type);
683 bt_get(event_context_type);
af181248
JG
684 stream_class->event_context_type = event_context_type;
685end:
686 return ret;
687}
688
11b0cdc8
JG
689void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
690{
83509119 691 bt_get(stream_class);
11b0cdc8
JG
692}
693
694void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
695{
83509119 696 bt_put(stream_class);
11b0cdc8
JG
697}
698
699BT_HIDDEN
700void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
701{
702 if (!stream_class) {
703 return;
704 }
705
706 stream_class->frozen = 1;
662e778c 707 bt_ctf_field_type_freeze(stream_class->event_header_type);
12c8a1a3 708 bt_ctf_field_type_freeze(stream_class->packet_context_type);
af181248 709 bt_ctf_field_type_freeze(stream_class->event_context_type);
11b0cdc8 710 bt_ctf_clock_freeze(stream_class->clock);
11b0cdc8
JG
711}
712
11b0cdc8 713BT_HIDDEN
445c3471
PP
714void bt_ctf_stream_class_set_byte_order(
715 struct bt_ctf_stream_class *stream_class, int byte_order)
11b0cdc8 716{
445c3471 717 int i;
11b0cdc8 718
445c3471
PP
719 assert(stream_class);
720 assert(byte_order == LITTLE_ENDIAN || byte_order == BIG_ENDIAN);
721 stream_class->byte_order = byte_order;
5ca83563
JG
722
723 /* Set native byte order to little or big endian */
724 bt_ctf_field_type_set_native_byte_order(
445c3471 725 stream_class->event_header_type, byte_order);
5ca83563 726 bt_ctf_field_type_set_native_byte_order(
445c3471 727 stream_class->packet_context_type, byte_order);
5ca83563 728 bt_ctf_field_type_set_native_byte_order(
445c3471 729 stream_class->event_context_type, byte_order);
5ca83563
JG
730
731 /* Set all events' native byte order */
732 for (i = 0; i < stream_class->event_classes->len; i++) {
445c3471
PP
733 struct bt_ctf_event_class *event_class =
734 g_ptr_array_index(stream_class->event_classes, i);
9849f6ff 735
9849f6ff 736 bt_ctf_event_class_set_native_byte_order(event_class,
445c3471 737 byte_order);
5ca83563 738 }
11b0cdc8
JG
739}
740
741BT_HIDDEN
742int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
743 struct metadata_context *context)
744{
2f100782 745 int64_t ret = 0;
11b0cdc8
JG
746 size_t i;
747
748 g_string_assign(context->field_name, "");
749 context->current_indentation_level = 1;
750 if (!stream_class->id_set) {
751 ret = -1;
752 goto end;
753 }
754
755 g_string_append_printf(context->string,
756 "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ",
757 stream_class->id);
758 ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
759 context);
760 if (ret) {
761 goto end;
762 }
763
764 g_string_append(context->string, ";\n\n\tpacket.context := ");
765 ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
766 context);
767 if (ret) {
768 goto end;
769 }
770
771 if (stream_class->event_context_type) {
772 g_string_append(context->string, ";\n\n\tevent.context := ");
773 ret = bt_ctf_field_type_serialize(
774 stream_class->event_context_type, context);
775 if (ret) {
776 goto end;
777 }
778 }
779
780 g_string_append(context->string, ";\n};\n\n");
11b0cdc8
JG
781 for (i = 0; i < stream_class->event_classes->len; i++) {
782 struct bt_ctf_event_class *event_class =
783 stream_class->event_classes->pdata[i];
784
11b0cdc8
JG
785 ret = bt_ctf_event_class_serialize(event_class, context);
786 if (ret) {
787 goto end;
788 }
789 }
790end:
791 context->current_indentation_level = 0;
792 return ret;
793}
794
795static
83509119 796void bt_ctf_stream_class_destroy(struct bt_object *obj)
11b0cdc8
JG
797{
798 struct bt_ctf_stream_class *stream_class;
799
83509119
JG
800 stream_class = container_of(obj, struct bt_ctf_stream_class, base);
801 bt_put(stream_class->clock);
11b0cdc8
JG
802
803 if (stream_class->event_classes) {
804 g_ptr_array_free(stream_class->event_classes, TRUE);
805 }
806
807 if (stream_class->name) {
808 g_string_free(stream_class->name, TRUE);
809 }
810
83509119
JG
811 bt_put(stream_class->event_header_type);
812 bt_put(stream_class->packet_context_type);
813 bt_put(stream_class->event_context_type);
11b0cdc8
JG
814 g_free(stream_class);
815}
816
817static
662e778c 818int init_event_header(struct bt_ctf_stream_class *stream_class)
11b0cdc8
JG
819{
820 int ret = 0;
821 struct bt_ctf_field_type *event_header_type =
822 bt_ctf_field_type_structure_create();
823 struct bt_ctf_field_type *_uint32_t =
824 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
825 struct bt_ctf_field_type *_uint64_t =
826 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
827
828 if (!event_header_type) {
829 ret = -1;
830 goto end;
831 }
832
11b0cdc8
JG
833 ret = bt_ctf_field_type_structure_add_field(event_header_type,
834 _uint32_t, "id");
835 if (ret) {
836 goto end;
837 }
838
839 ret = bt_ctf_field_type_structure_add_field(event_header_type,
840 _uint64_t, "timestamp");
841 if (ret) {
842 goto end;
843 }
844
662e778c 845 if (stream_class->event_header_type) {
83509119 846 bt_put(stream_class->event_header_type);
de876b7f 847 }
662e778c 848 stream_class->event_header_type = event_header_type;
11b0cdc8
JG
849end:
850 if (ret) {
83509119 851 bt_put(event_header_type);
11b0cdc8
JG
852 }
853
83509119
JG
854 bt_put(_uint32_t);
855 bt_put(_uint64_t);
11b0cdc8
JG
856 return ret;
857}
858
859static
662e778c 860int init_packet_context(struct bt_ctf_stream_class *stream_class)
11b0cdc8
JG
861{
862 int ret = 0;
863 struct bt_ctf_field_type *packet_context_type =
864 bt_ctf_field_type_structure_create();
865 struct bt_ctf_field_type *_uint64_t =
866 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
867
868 if (!packet_context_type) {
869 ret = -1;
870 goto end;
871 }
872
873 /*
874 * We create a stream packet context as proposed in the CTF
875 * specification.
876 */
11b0cdc8
JG
877 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
878 _uint64_t, "timestamp_begin");
879 if (ret) {
880 goto end;
881 }
882
883 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
884 _uint64_t, "timestamp_end");
885 if (ret) {
886 goto end;
887 }
888
889 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
890 _uint64_t, "content_size");
891 if (ret) {
892 goto end;
893 }
894
895 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
896 _uint64_t, "packet_size");
897 if (ret) {
898 goto end;
899 }
900
901 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
902 _uint64_t, "events_discarded");
903 if (ret) {
904 goto end;
905 }
906
83509119 907 bt_put(stream_class->packet_context_type);
11b0cdc8 908 stream_class->packet_context_type = packet_context_type;
11b0cdc8
JG
909end:
910 if (ret) {
83509119 911 bt_put(packet_context_type);
11b0cdc8
JG
912 goto end;
913 }
914
83509119 915 bt_put(_uint64_t);
11b0cdc8
JG
916 return ret;
917}
This page took 0.068923 seconds and 4 git commands to generate.