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