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