Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[babeltrace.git] / src / ctf-writer / stream-class.c
CommitLineData
3dca2276
PP
1/*
2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
350ad6c1 24#define BT_LOG_TAG "CTF-WRITER/STREAM-CLASS"
67d2ce02 25#include "logging.h"
3dca2276 26
3dca2276 27#include <inttypes.h>
16ca5ff0
PP
28#include <stdint.h>
29
217cf9d3
PP
30#include <babeltrace2-ctf-writer/event.h>
31#include <babeltrace2-ctf-writer/object.h>
32#include <babeltrace2-ctf-writer/trace.h>
33#include <babeltrace2-ctf-writer/utils.h>
578e048b
MJ
34
35#include "common/align.h"
36#include "common/assert.h"
37#include "compat/compiler.h"
38#include "compat/endian.h"
39
40#include "assert-pre.h"
41#include "clock-class.h"
42#include "event-class.h"
43#include "event.h"
44#include "fields.h"
45#include "field-types.h"
46#include "field-wrapper.h"
47#include "stream-class.h"
48#include "utils.h"
49#include "validation.h"
50#include "visitor.h"
51#include "writer.h"
52
16ca5ff0
PP
53BT_HIDDEN
54int bt_ctf_stream_class_common_initialize(struct bt_ctf_stream_class_common *stream_class,
e1e02a22 55 const char *name, bt_ctf_object_release_func release_func)
16ca5ff0
PP
56{
57 BT_LOGD("Initializing common stream class object: name=\"%s\"", name);
58
e1e02a22 59 bt_ctf_object_init_shared_with_parent(&stream_class->base, release_func);
16ca5ff0
PP
60 stream_class->name = g_string_new(name);
61 stream_class->event_classes = g_ptr_array_new_with_free_func(
e1e02a22 62 (GDestroyNotify) bt_ctf_object_try_spec_release);
16ca5ff0
PP
63 if (!stream_class->event_classes) {
64 BT_LOGE_STR("Failed to allocate a GPtrArray.");
65 goto error;
66 }
67
68 stream_class->event_classes_ht = g_hash_table_new_full(g_int64_hash,
69 g_int64_equal, g_free, NULL);
70 if (!stream_class->event_classes_ht) {
71 BT_LOGE_STR("Failed to allocate a GHashTable.");
72 goto error;
73 }
74
75 BT_LOGD("Initialized common stream class object: addr=%p, name=\"%s\"",
76 stream_class, name);
77 return 0;
78
79error:
80 return -1;
81}
82
83BT_HIDDEN
84void bt_ctf_stream_class_common_finalize(struct bt_ctf_stream_class_common *stream_class)
85{
86 BT_LOGD("Finalizing common stream class: addr=%p, name=\"%s\", id=%" PRId64,
87 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
88 bt_ctf_stream_class_common_get_id(stream_class));
e1e02a22 89 bt_ctf_object_put_ref(stream_class->clock_class);
16ca5ff0
PP
90
91 if (stream_class->event_classes_ht) {
92 g_hash_table_destroy(stream_class->event_classes_ht);
93 }
94 if (stream_class->event_classes) {
95 BT_LOGD_STR("Destroying event classes.");
96 g_ptr_array_free(stream_class->event_classes, TRUE);
97 }
98
99 if (stream_class->name) {
100 g_string_free(stream_class->name, TRUE);
101 }
102
103 BT_LOGD_STR("Putting event header field type.");
e1e02a22 104 bt_ctf_object_put_ref(stream_class->event_header_field_type);
16ca5ff0 105 BT_LOGD_STR("Putting packet context field type.");
e1e02a22 106 bt_ctf_object_put_ref(stream_class->packet_context_field_type);
16ca5ff0 107 BT_LOGD_STR("Putting event context field type.");
e1e02a22 108 bt_ctf_object_put_ref(stream_class->event_context_field_type);
16ca5ff0
PP
109}
110
111static
112void event_class_exists(gpointer element, gpointer query)
113{
114 struct bt_ctf_event_class_common *event_class_a = element;
115 struct bt_ctf_search_query *search_query = query;
116 struct bt_ctf_event_class_common *event_class_b = search_query->value;
117 int64_t id_a, id_b;
118
119 if (search_query->value == element) {
120 search_query->found = 1;
121 goto end;
122 }
123
124 /*
125 * Two event classes cannot share the same ID in a given
126 * stream class.
127 */
128 id_a = bt_ctf_event_class_common_get_id(event_class_a);
129 id_b = bt_ctf_event_class_common_get_id(event_class_b);
130
131 if (id_a < 0 || id_b < 0) {
132 /* at least one ID is not set: will be automatically set later */
133 goto end;
134 }
135
136 if (id_a == id_b) {
137 BT_LOGW("Event class with this ID already exists in the stream class: "
138 "id=%" PRId64 ", name=\"%s\"",
139 id_a, bt_ctf_event_class_common_get_name(event_class_a));
140 search_query->found = 1;
141 goto end;
142 }
143
144end:
145 return;
146}
147
148BT_HIDDEN
149int bt_ctf_stream_class_common_add_event_class(
150 struct bt_ctf_stream_class_common *stream_class,
151 struct bt_ctf_event_class_common *event_class,
152 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func)
153{
154 int ret = 0;
155 int64_t *event_id = NULL;
156 struct bt_ctf_trace_common *trace = NULL;
157 struct bt_ctf_stream_class_common *old_stream_class = NULL;
158 struct bt_ctf_validation_output validation_output = { 0 };
159 struct bt_ctf_field_type_common *packet_header_type = NULL;
160 struct bt_ctf_field_type_common *packet_context_type = NULL;
161 struct bt_ctf_field_type_common *event_header_type = NULL;
162 struct bt_ctf_field_type_common *stream_event_ctx_type = NULL;
163 struct bt_ctf_field_type_common *event_context_type = NULL;
164 struct bt_ctf_field_type_common *event_payload_type = NULL;
165 const enum bt_ctf_validation_flag validation_flags =
166 BT_CTF_VALIDATION_FLAG_EVENT;
167 struct bt_ctf_clock_class *expected_clock_class = NULL;
96741e7f 168 struct bt_ctf_search_query query = { .value = event_class, .found = 0 };
16ca5ff0 169
98b15851 170 BT_ASSERT_DBG(copy_field_type_func);
16ca5ff0
PP
171
172 if (!stream_class || !event_class) {
173 BT_LOGW("Invalid parameter: stream class or event class is NULL: "
174 "stream-class-addr=%p, event-class-addr=%p",
175 stream_class, event_class);
176 ret = -1;
177 goto end;
178 }
179
180 BT_LOGD("Adding event class to stream class: "
181 "stream-class-addr=%p, stream-class-name=\"%s\", "
182 "stream-class-id=%" PRId64 ", event-class-addr=%p, "
183 "event-class-name=\"%s\", event-class-id=%" PRId64,
184 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
185 bt_ctf_stream_class_common_get_id(stream_class),
186 event_class,
187 bt_ctf_event_class_common_get_name(event_class),
188 bt_ctf_event_class_common_get_id(event_class));
189 trace = bt_ctf_stream_class_common_borrow_trace(stream_class);
190
191 if (stream_class->frozen) {
192 /*
193 * We only check that the event class to be added has a
194 * single class which matches the stream class's
195 * expected clock class if the stream class is frozen.
196 * If it's not, then this event class is added "as is"
197 * and the validation will be performed when calling
198 * either bt_ctf_trace_add_stream_class() or
199 * bt_ctf_event_create(). This is because the stream class's
200 * field types (packet context, event header, event
201 * context) could change before the next call to one of
202 * those two functions.
203 */
e1e02a22 204 expected_clock_class = bt_ctf_object_get_ref(stream_class->clock_class);
16ca5ff0
PP
205
206 /*
207 * At this point, `expected_clock_class` can be NULL,
208 * and bt_ctf_event_class_validate_single_clock_class()
209 * below can set it.
210 */
211 ret = bt_ctf_event_class_common_validate_single_clock_class(
212 event_class, &expected_clock_class);
213 if (ret) {
214 BT_LOGW("Event class contains a field type which is not "
215 "recursively mapped to its stream class's "
216 "expected clock class: "
217 "stream-class-addr=%p, "
218 "stream-class-id=%" PRId64 ", "
219 "stream-class-name=\"%s\", "
220 "expected-clock-class-addr=%p, "
221 "expected-clock-class-name=\"%s\"",
222 stream_class,
223 bt_ctf_stream_class_common_get_id(stream_class),
224 bt_ctf_stream_class_common_get_name(stream_class),
225 expected_clock_class,
226 expected_clock_class ?
227 bt_ctf_clock_class_get_name(expected_clock_class) :
228 NULL);
229 goto end;
230 }
231 }
232
233 event_id = g_new(int64_t, 1);
234 if (!event_id) {
235 BT_LOGE_STR("Failed to allocate one int64_t.");
236 ret = -1;
237 goto end;
238 }
239
240 /* Check for duplicate event classes */
16ca5ff0
PP
241 g_ptr_array_foreach(stream_class->event_classes, event_class_exists,
242 &query);
243 if (query.found) {
244 BT_LOGW_STR("Another event class part of this stream class has the same ID.");
245 ret = -1;
246 goto end;
247 }
248
249 old_stream_class = bt_ctf_event_class_common_borrow_stream_class(event_class);
250 if (old_stream_class) {
251 /* Event class is already associated to a stream class. */
252 BT_LOGW("Event class is already part of another stream class: "
253 "event-class-stream-class-addr=%p, "
254 "event-class-stream-class-name=\"%s\", "
255 "event-class-stream-class-id=%" PRId64,
256 old_stream_class,
257 bt_ctf_stream_class_common_get_name(old_stream_class),
258 bt_ctf_stream_class_common_get_id(old_stream_class));
259 ret = -1;
260 goto end;
261 }
262
263 if (trace) {
264 /*
265 * If the stream class is associated with a trace, then
266 * both those objects are frozen. Also, this event class
267 * is about to be frozen.
268 *
269 * Therefore the event class must be validated here.
270 * The trace and stream class should be valid at this
271 * point.
272 */
98b15851
PP
273 BT_ASSERT_DBG(trace->valid);
274 BT_ASSERT_DBG(stream_class->valid);
16ca5ff0
PP
275 packet_header_type =
276 bt_ctf_trace_common_borrow_packet_header_field_type(trace);
277 packet_context_type =
278 bt_ctf_stream_class_common_borrow_packet_context_field_type(
279 stream_class);
280 event_header_type =
281 bt_ctf_stream_class_common_borrow_event_header_field_type(
282 stream_class);
283 stream_event_ctx_type =
284 bt_ctf_stream_class_common_borrow_event_context_field_type(
285 stream_class);
286 event_context_type =
287 bt_ctf_event_class_common_borrow_context_field_type(
288 event_class);
289 event_payload_type =
290 bt_ctf_event_class_common_borrow_payload_field_type(
291 event_class);
292 ret = bt_ctf_validate_class_types(
293 trace->environment, packet_header_type,
294 packet_context_type, event_header_type,
295 stream_event_ctx_type, event_context_type,
296 event_payload_type, trace->valid,
297 stream_class->valid, event_class->valid,
298 &validation_output, validation_flags,
299 copy_field_type_func);
300
301 if (ret) {
302 /*
303 * This means something went wrong during the
304 * validation process, not that the objects are
305 * invalid.
306 */
307 BT_LOGE("Failed to validate event class: ret=%d", ret);
308 goto end;
309 }
310
311 if ((validation_output.valid_flags & validation_flags) !=
312 validation_flags) {
313 /* Invalid event class */
314 BT_LOGW("Invalid trace, stream class, or event class: "
315 "valid-flags=0x%x",
316 validation_output.valid_flags);
317 ret = -1;
318 goto end;
319 }
320 }
321
322 /* Only set an event ID if none was explicitly set before */
323 *event_id = bt_ctf_event_class_common_get_id(event_class);
324 if (*event_id < 0) {
ef267d12 325 BT_LOGT("Event class has no ID: automatically setting it: "
16ca5ff0
PP
326 "id=%" PRId64, stream_class->next_event_id);
327
328 if (bt_ctf_event_class_common_set_id(event_class,
329 stream_class->next_event_id)) {
330 BT_LOGE("Cannot set event class's ID: id=%" PRId64,
331 stream_class->next_event_id);
332 ret = -1;
333 goto end;
334 }
335 stream_class->next_event_id++;
336 *event_id = stream_class->next_event_id;
337 }
338
e1e02a22 339 bt_ctf_object_set_parent(&event_class->base, &stream_class->base);
16ca5ff0
PP
340
341 if (trace) {
342 /*
343 * At this point we know that the function will be
344 * successful. Therefore we can replace the event
345 * class's field types with what's in the validation
346 * output structure and mark this event class as valid.
347 */
348 bt_ctf_validation_replace_types(NULL, NULL, event_class,
349 &validation_output, validation_flags);
350 event_class->valid = 1;
351
352 /*
353 * Put what was not moved in
354 * bt_ctf_validation_replace_types().
355 */
356 bt_ctf_validation_output_put_types(&validation_output);
357 }
358
359 /* Add to the event classes of the stream class */
360 g_ptr_array_add(stream_class->event_classes, event_class);
361 g_hash_table_insert(stream_class->event_classes_ht, event_id,
362 event_class);
363 event_id = NULL;
364
365 /* Freeze the event class */
366 bt_ctf_event_class_common_freeze(event_class);
367
368 /*
369 * It is safe to set the stream class's unique clock class
370 * now if the stream class is frozen.
371 */
372 if (stream_class->frozen && expected_clock_class) {
98b15851 373 BT_ASSERT_DBG(!stream_class->clock_class ||
16ca5ff0 374 stream_class->clock_class == expected_clock_class);
e1e02a22 375 BT_CTF_OBJECT_MOVE_REF(stream_class->clock_class, expected_clock_class);
16ca5ff0
PP
376 }
377
378 BT_LOGD("Added event class to stream class: "
379 "stream-class-addr=%p, stream-class-name=\"%s\", "
380 "stream-class-id=%" PRId64 ", event-class-addr=%p, "
381 "event-class-name=\"%s\", event-class-id=%" PRId64,
382 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
383 bt_ctf_stream_class_common_get_id(stream_class),
384 event_class,
385 bt_ctf_event_class_common_get_name(event_class),
386 bt_ctf_event_class_common_get_id(event_class));
387
388end:
389 bt_ctf_validation_output_put_types(&validation_output);
e1e02a22 390 bt_ctf_object_put_ref(expected_clock_class);
16ca5ff0
PP
391 g_free(event_id);
392 return ret;
393}
394
395static
396int64_t get_event_class_count(void *element)
397{
398 return bt_ctf_stream_class_get_event_class_count(
399 (struct bt_ctf_stream_class *) element);
400}
401
402static
403void *get_event_class(void *element, int i)
404{
405 return bt_ctf_stream_class_get_event_class_by_index(
406 (struct bt_ctf_stream_class *) element, i);
407}
408
409static
410int visit_event_class(void *object, bt_ctf_visitor visitor,void *data)
411{
412 struct bt_ctf_visitor_object obj = {
413 .object = object,
414 .type = BT_CTF_VISITOR_OBJECT_TYPE_EVENT_CLASS
415 };
416
417 return visitor(&obj, data);
418}
419
420BT_HIDDEN
421int bt_ctf_stream_class_common_visit(struct bt_ctf_stream_class_common *stream_class,
422 bt_ctf_visitor visitor, void *data)
423{
424 int ret;
425 struct bt_ctf_visitor_object obj = {
426 .object = stream_class,
427 .type = BT_CTF_VISITOR_OBJECT_TYPE_STREAM_CLASS
428 };
429
430 if (!stream_class || !visitor) {
431 BT_LOGW("Invalid parameter: stream class or visitor is NULL: "
432 "stream-class-addr=%p, visitor=%p",
433 stream_class, visitor);
434 ret = -1;
435 goto end;
436 }
437
44c440bc 438 ret = bt_ctf_visitor_helper(&obj, get_event_class_count,
16ca5ff0
PP
439 get_event_class,
440 visit_event_class, visitor, data);
ef267d12 441 BT_LOGT("bt_ctf_visitor_helper() returned: ret=%d", ret);
16ca5ff0
PP
442
443end:
444 return ret;
445}
446
447BT_HIDDEN
448int bt_ctf_stream_class_visit(struct bt_ctf_stream_class *stream_class,
449 bt_ctf_visitor visitor, void *data)
450{
451 return bt_ctf_stream_class_common_visit(BT_CTF_FROM_COMMON(stream_class),
452 visitor, data);
453}
454
455BT_HIDDEN
456void bt_ctf_stream_class_common_freeze(struct bt_ctf_stream_class_common *stream_class)
457{
458 if (!stream_class || stream_class->frozen) {
459 return;
460 }
461
462 BT_LOGD("Freezing stream class: addr=%p, name=\"%s\", id=%" PRId64,
463 stream_class, bt_ctf_stream_class_common_get_name(stream_class),
464 bt_ctf_stream_class_common_get_id(stream_class));
465 stream_class->frozen = 1;
466 bt_ctf_field_type_common_freeze(stream_class->event_header_field_type);
467 bt_ctf_field_type_common_freeze(stream_class->packet_context_field_type);
468 bt_ctf_field_type_common_freeze(stream_class->event_context_field_type);
469 bt_ctf_clock_class_freeze(stream_class->clock_class);
470}
471
472BT_HIDDEN
473int bt_ctf_stream_class_common_validate_single_clock_class(
474 struct bt_ctf_stream_class_common *stream_class,
475 struct bt_ctf_clock_class **expected_clock_class)
476{
477 int ret;
478 uint64_t i;
479
98b15851
PP
480 BT_ASSERT_DBG(stream_class);
481 BT_ASSERT_DBG(expected_clock_class);
16ca5ff0
PP
482 ret = bt_ctf_field_type_common_validate_single_clock_class(
483 stream_class->packet_context_field_type,
484 expected_clock_class);
485 if (ret) {
486 BT_LOGW("Stream class's packet context field type "
487 "is not recursively mapped to the "
488 "expected clock class: "
489 "stream-class-addr=%p, "
490 "stream-class-name=\"%s\", "
491 "stream-class-id=%" PRId64 ", "
492 "ft-addr=%p",
493 stream_class,
494 bt_ctf_stream_class_common_get_name(stream_class),
495 stream_class->id,
496 stream_class->packet_context_field_type);
497 goto end;
498 }
499
500 ret = bt_ctf_field_type_common_validate_single_clock_class(
501 stream_class->event_header_field_type,
502 expected_clock_class);
503 if (ret) {
504 BT_LOGW("Stream class's event header field type "
505 "is not recursively mapped to the "
506 "expected clock class: "
507 "stream-class-addr=%p, "
508 "stream-class-name=\"%s\", "
509 "stream-class-id=%" PRId64 ", "
510 "ft-addr=%p",
511 stream_class,
512 bt_ctf_stream_class_common_get_name(stream_class),
513 stream_class->id,
514 stream_class->event_header_field_type);
515 goto end;
516 }
517
518 ret = bt_ctf_field_type_common_validate_single_clock_class(
519 stream_class->event_context_field_type,
520 expected_clock_class);
521 if (ret) {
522 BT_LOGW("Stream class's event context field type "
523 "is not recursively mapped to the "
524 "expected clock class: "
525 "stream-class-addr=%p, "
526 "stream-class-name=\"%s\", "
527 "stream-class-id=%" PRId64 ", "
528 "ft-addr=%p",
529 stream_class,
530 bt_ctf_stream_class_common_get_name(stream_class),
531 stream_class->id,
532 stream_class->event_context_field_type);
533 goto end;
534 }
535
536 for (i = 0; i < stream_class->event_classes->len; i++) {
537 struct bt_ctf_event_class_common *event_class =
538 g_ptr_array_index(stream_class->event_classes, i);
539
98b15851 540 BT_ASSERT_DBG(event_class);
16ca5ff0
PP
541 ret = bt_ctf_event_class_common_validate_single_clock_class(
542 event_class, expected_clock_class);
543 if (ret) {
544 BT_LOGW("Stream class's event class contains a "
545 "field type which is not recursively mapped to "
546 "the expected clock class: "
547 "stream-class-addr=%p, "
548 "stream-class-name=\"%s\", "
549 "stream-class-id=%" PRId64,
550 stream_class,
551 bt_ctf_stream_class_common_get_name(stream_class),
552 stream_class->id);
553 goto end;
554 }
555 }
556
557end:
558 return ret;
559}
3dca2276
PP
560
561static
562int init_event_header(struct bt_ctf_stream_class *stream_class)
563{
564 int ret = 0;
565 struct bt_ctf_field_type *event_header_type =
566 bt_ctf_field_type_structure_create();
567 struct bt_ctf_field_type *_uint32_t =
568 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
569 struct bt_ctf_field_type *_uint64_t =
570 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
571
572 if (!event_header_type) {
573 BT_LOGE_STR("Cannot create empty structure field type.");
574 ret = -1;
575 goto end;
576 }
577
578 ret = bt_ctf_field_type_structure_add_field(event_header_type,
579 _uint32_t, "id");
580 if (ret) {
581 BT_LOGE_STR("Cannot add `id` field to event header field type.");
582 goto end;
583 }
584
585 ret = bt_ctf_field_type_structure_add_field(event_header_type,
586 _uint64_t, "timestamp");
587 if (ret) {
588 BT_LOGE_STR("Cannot add `timestamp` field to event header field type.");
589 goto end;
590 }
591
e1e02a22 592 bt_ctf_object_put_ref(stream_class->common.event_header_field_type);
3dca2276
PP
593 stream_class->common.event_header_field_type =
594 (void *) event_header_type;
595 event_header_type = NULL;
596
597end:
598 if (ret) {
e1e02a22 599 bt_ctf_object_put_ref(event_header_type);
3dca2276
PP
600 }
601
e1e02a22
PP
602 bt_ctf_object_put_ref(_uint32_t);
603 bt_ctf_object_put_ref(_uint64_t);
3dca2276
PP
604 return ret;
605}
606
607static
608int init_packet_context(struct bt_ctf_stream_class *stream_class)
609{
610 int ret = 0;
611 struct bt_ctf_field_type *packet_context_type =
612 bt_ctf_field_type_structure_create();
613 struct bt_ctf_field_type *_uint64_t =
614 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
4af85094 615 struct bt_ctf_field_type *ts_begin_end_uint64_t = NULL;
3dca2276
PP
616
617 if (!packet_context_type) {
618 BT_LOGE_STR("Cannot create empty structure field type.");
619 ret = -1;
620 goto end;
621 }
622
623 ts_begin_end_uint64_t = bt_ctf_field_type_copy(_uint64_t);
624 if (!ts_begin_end_uint64_t) {
625 BT_LOGE_STR("Cannot copy integer field type for `timestamp_begin` and `timestamp_end` fields.");
626 ret = -1;
627 goto end;
628 }
629
630 /*
631 * We create a stream packet context as proposed in the CTF
632 * specification.
633 */
634 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
635 ts_begin_end_uint64_t, "timestamp_begin");
636 if (ret) {
637 BT_LOGE_STR("Cannot add `timestamp_begin` field to event header field type.");
638 goto end;
639 }
640
641 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
642 ts_begin_end_uint64_t, "timestamp_end");
643 if (ret) {
644 BT_LOGE_STR("Cannot add `timestamp_end` field to event header field type.");
645 goto end;
646 }
647
648 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
649 _uint64_t, "content_size");
650 if (ret) {
651 BT_LOGE_STR("Cannot add `content_size` field to event header field type.");
652 goto end;
653 }
654
655 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
656 _uint64_t, "packet_size");
657 if (ret) {
658 BT_LOGE_STR("Cannot add `packet_size` field to event header field type.");
659 goto end;
660 }
661
662 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
663 _uint64_t, "events_discarded");
664 if (ret) {
665 BT_LOGE_STR("Cannot add `events_discarded` field to event header field type.");
666 goto end;
667 }
668
e1e02a22 669 bt_ctf_object_put_ref(stream_class->common.packet_context_field_type);
3dca2276
PP
670 stream_class->common.packet_context_field_type =
671 (void *) packet_context_type;
672 packet_context_type = NULL;
673
674end:
675 if (ret) {
e1e02a22 676 bt_ctf_object_put_ref(packet_context_type);
3dca2276
PP
677 goto end;
678 }
679
e1e02a22
PP
680 bt_ctf_object_put_ref(_uint64_t);
681 bt_ctf_object_put_ref(ts_begin_end_uint64_t);
3dca2276
PP
682 return ret;
683}
684
685static
e1e02a22 686void bt_ctf_stream_class_destroy(struct bt_ctf_object *obj)
3dca2276
PP
687{
688 struct bt_ctf_stream_class *stream_class;
689
690 stream_class = (void *) obj;
691 BT_LOGD("Destroying CTF writer stream class: addr=%p, name=\"%s\", id=%" PRId64,
692 stream_class, bt_ctf_stream_class_get_name(stream_class),
693 bt_ctf_stream_class_get_id(stream_class));
16ca5ff0 694 bt_ctf_stream_class_common_finalize(BT_CTF_TO_COMMON(stream_class));
e1e02a22 695 bt_ctf_object_put_ref(stream_class->clock);
3dca2276
PP
696 g_free(stream_class);
697}
698
699struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
700{
701 struct bt_ctf_stream_class *stream_class;
702 int ret;
703
704 BT_LOGD("Creating CTF writer stream class object: name=\"%s\"", name);
705 stream_class = g_new0(struct bt_ctf_stream_class, 1);
706 if (!stream_class) {
707 BT_LOGE_STR("Failed to allocate one CTF writer stream class.");
708 goto error;
709 }
710
16ca5ff0 711 ret = bt_ctf_stream_class_common_initialize(BT_CTF_TO_COMMON(stream_class),
3dca2276
PP
712 name, bt_ctf_stream_class_destroy);
713 if (ret) {
16ca5ff0 714 /* bt_ctf_stream_class_common_initialize() logs errors */
3dca2276
PP
715 goto error;
716 }
717
718 ret = init_event_header(stream_class);
719 if (ret) {
720 BT_LOGE_STR("Cannot initialize stream class's event header field type.");
721 goto error;
722 }
723
724 ret = init_packet_context(stream_class);
725 if (ret) {
726 BT_LOGE_STR("Cannot initialize stream class's packet context field type.");
727 goto error;
728 }
729
730 BT_LOGD("Created CTF writer stream class object: addr=%p, name=\"%s\"",
731 stream_class, name);
732 return stream_class;
733
734error:
e1e02a22 735 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream_class);
3dca2276
PP
736 return stream_class;
737}
738
739static
740int try_map_clock_class(struct bt_ctf_stream_class *stream_class,
741 struct bt_ctf_field_type *parent_ft, const char *field_name)
742{
743 struct bt_ctf_clock_class *mapped_clock_class = NULL;
744 int ret = 0;
745 struct bt_ctf_field_type *ft =
746 bt_ctf_field_type_structure_get_field_type_by_name(parent_ft,
747 field_name);
748
98b15851 749 BT_ASSERT_DBG(stream_class->clock);
3dca2276
PP
750
751 if (!ft) {
752 /* Field does not exist: not an error */
753 goto end;
754 }
755
98b15851 756 BT_ASSERT_DBG(((struct bt_ctf_field_type_common *) ft)->id ==
16ca5ff0 757 BT_CTF_FIELD_TYPE_ID_INTEGER);
3dca2276
PP
758 mapped_clock_class =
759 bt_ctf_field_type_integer_get_mapped_clock_class(ft);
760 if (!mapped_clock_class) {
761 struct bt_ctf_field_type *ft_copy;
762
763 if (!stream_class->clock) {
764 BT_LOGW("Cannot automatically set field's type mapped clock class: stream class's clock is not set: "
765 "stream-class-addr=%p, stream-class-name=\"%s\", "
766 "stream-class-id=%" PRId64 ", ft-addr=%p",
767 stream_class,
768 bt_ctf_stream_class_get_name(stream_class),
769 bt_ctf_stream_class_get_id(stream_class), ft);
770 ret = -1;
771 goto end;
772 }
773
774 ft_copy = bt_ctf_field_type_copy(ft);
775 if (!ft_copy) {
776 BT_LOGE("Failed to copy integer field type: ft-addr=%p",
777 ft);
94c1a13b
FD
778 ret = -1;
779 goto end;
3dca2276
PP
780 }
781
16ca5ff0
PP
782 ret = bt_ctf_field_type_common_integer_set_mapped_clock_class_no_check_frozen(
783 (void *) ft_copy, stream_class->clock->clock_class);
98b15851 784 BT_ASSERT_DBG(ret == 0);
3dca2276 785
16ca5ff0 786 ret = bt_ctf_field_type_common_structure_replace_field(
3dca2276 787 (void *) parent_ft, field_name, (void *) ft_copy);
e1e02a22 788 bt_ctf_object_put_ref(ft_copy);
ef267d12 789 BT_LOGT("Automatically mapped field type to stream class's clock class: "
3dca2276
PP
790 "stream-class-addr=%p, stream-class-name=\"%s\", "
791 "stream-class-id=%" PRId64 ", ft-addr=%p, "
792 "ft-copy-addr=%p",
793 stream_class,
794 bt_ctf_stream_class_get_name(stream_class),
795 bt_ctf_stream_class_get_id(stream_class), ft, ft_copy);
796 }
797
798end:
e1e02a22
PP
799 bt_ctf_object_put_ref(ft);
800 bt_ctf_object_put_ref(mapped_clock_class);
3dca2276
PP
801 return ret;
802}
803
804BT_HIDDEN
805int bt_ctf_stream_class_map_clock_class(
806 struct bt_ctf_stream_class *stream_class,
807 struct bt_ctf_field_type *packet_context_type,
808 struct bt_ctf_field_type *event_header_type)
809{
810 int ret = 0;
811
98b15851 812 BT_ASSERT_DBG(stream_class);
3dca2276
PP
813
814 if (!stream_class->clock) {
815 /* No clock class to map to */
816 goto end;
817 }
818
819 if (packet_context_type) {
820 if (try_map_clock_class(stream_class, packet_context_type,
821 "timestamp_begin")) {
822 BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_begin` field's mapped clock class.");
823 ret = -1;
824 goto end;
825 }
826
827 if (try_map_clock_class(stream_class, packet_context_type,
828 "timestamp_end")) {
829 BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_end` field's mapped clock class.");
830 ret = -1;
831 goto end;
832 }
833 }
834
835 if (event_header_type) {
836 if (try_map_clock_class(stream_class, event_header_type,
837 "timestamp")) {
838 BT_LOGE_STR("Cannot automatically set stream class's event header field type's `timestamp` field's mapped clock class.");
839 ret = -1;
840 goto end;
841 }
842 }
843
844end:
845 return ret;
846}
847
848struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
849 struct bt_ctf_stream_class *stream_class)
850{
851 struct bt_ctf_clock *clock = NULL;
852
853 if (!stream_class) {
854 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
855 goto end;
856 }
857
858 if (!stream_class->clock) {
ef267d12 859 BT_LOGT("Stream class has no clock: "
3dca2276
PP
860 "addr=%p, name=\"%s\", id=%" PRId64,
861 stream_class,
862 bt_ctf_stream_class_get_name(stream_class),
863 bt_ctf_stream_class_get_id(stream_class));
864 goto end;
865 }
866
e1e02a22 867 clock = bt_ctf_object_get_ref(stream_class->clock);
3dca2276
PP
868
869end:
870 return clock;
871}
872
873int bt_ctf_stream_class_set_clock(
874 struct bt_ctf_stream_class *stream_class,
875 struct bt_ctf_clock *clock)
876{
877 int ret = 0;
878
879 if (!stream_class || !clock) {
880 BT_LOGW("Invalid parameter: stream class or clock is NULL: "
881 "stream-class-addr=%p, clock-addr=%p",
882 stream_class, clock);
883 ret = -1;
884 goto end;
885 }
886
887 if (stream_class->common.frozen) {
888 BT_LOGW("Invalid parameter: stream class is frozen: "
889 "addr=%p, name=\"%s\", id=%" PRId64,
890 stream_class,
891 bt_ctf_stream_class_get_name(stream_class),
892 bt_ctf_stream_class_get_id(stream_class));
893 ret = -1;
894 goto end;
895 }
896
897 /* Replace the current clock of this stream class. */
e1e02a22
PP
898 bt_ctf_object_put_ref(stream_class->clock);
899 stream_class->clock = bt_ctf_object_get_ref(clock);
ef267d12 900 BT_LOGT("Set stream class's clock: "
3dca2276
PP
901 "addr=%p, name=\"%s\", id=%" PRId64 ", "
902 "clock-addr=%p, clock-name=\"%s\"",
903 stream_class,
904 bt_ctf_stream_class_get_name(stream_class),
905 bt_ctf_stream_class_get_id(stream_class),
906 stream_class->clock,
907 bt_ctf_clock_get_name(stream_class->clock));
908
909end:
910 return ret;
911}
912
913BT_HIDDEN
914int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
915 struct metadata_context *context)
916{
917 int ret = 0;
918 size_t i;
919 struct bt_ctf_trace *trace;
920 struct bt_ctf_field_type *packet_header_type = NULL;
921
922 BT_LOGD("Serializing stream class's metadata: "
923 "stream-class-addr=%p, stream-class-name=\"%s\", "
924 "stream-class-id=%" PRId64 ", metadata-context-addr=%p",
925 stream_class,
926 bt_ctf_stream_class_get_name(stream_class),
927 bt_ctf_stream_class_get_id(stream_class), context);
928 g_string_assign(context->field_name, "");
929 context->current_indentation_level = 1;
930 if (!stream_class->common.id_set) {
931 BT_LOGW_STR("Stream class's ID is not set.");
932 ret = -1;
933 goto end;
934 }
935
936 g_string_append(context->string, "stream {\n");
937
938 /*
939 * The reference to the trace is only borrowed since the
940 * serialization of the stream class might have been triggered
941 * by the trace's destruction. In such a case, the trace's
942 * reference count would, unexepectedly, go through the sequence
943 * 1 -> 0 -> 1 -> 0 -> ..., provoking an endless loop of destruction
944 * and serialization.
945 */
16ca5ff0
PP
946 trace = BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
947 BT_CTF_TO_COMMON(stream_class)));
98b15851 948 BT_ASSERT_DBG(trace);
3dca2276
PP
949 packet_header_type = bt_ctf_trace_get_packet_header_field_type(trace);
950 trace = NULL;
951 if (packet_header_type) {
952 struct bt_ctf_field_type *stream_id_type;
953
954 stream_id_type =
955 bt_ctf_field_type_structure_get_field_type_by_name(
956 packet_header_type, "stream_id");
957 if (stream_id_type) {
958 /*
959 * Only set the stream's id if the trace's packet header
960 * contains a stream_id field. This field is only
961 * needed if the trace contains only one stream
962 * class.
963 */
964 g_string_append_printf(context->string,
965 "\tid = %" PRId64 ";\n",
966 stream_class->common.id);
967 }
e1e02a22 968 bt_ctf_object_put_ref(stream_id_type);
3dca2276
PP
969 }
970 if (stream_class->common.event_header_field_type) {
971 BT_LOGD_STR("Serializing stream class's event header field type's metadata.");
972 g_string_append(context->string, "\tevent.header := ");
973 ret = bt_ctf_field_type_serialize_recursive(
974 (void *) stream_class->common.event_header_field_type,
975 context);
976 if (ret) {
977 BT_LOGW("Cannot serialize stream class's event header field type's metadata: "
978 "ret=%d", ret);
979 goto end;
980 }
981 g_string_append(context->string, ";");
982 }
983
984
985 if (stream_class->common.packet_context_field_type) {
986 BT_LOGD_STR("Serializing stream class's packet context field type's metadata.");
987 g_string_append(context->string, "\n\n\tpacket.context := ");
988 ret = bt_ctf_field_type_serialize_recursive(
989 (void *) stream_class->common.packet_context_field_type,
990 context);
991 if (ret) {
992 BT_LOGW("Cannot serialize stream class's packet context field type's metadata: "
993 "ret=%d", ret);
994 goto end;
995 }
996 g_string_append(context->string, ";");
997 }
998
999 if (stream_class->common.event_context_field_type) {
1000 BT_LOGD_STR("Serializing stream class's event context field type's metadata.");
1001 g_string_append(context->string, "\n\n\tevent.context := ");
1002 ret = bt_ctf_field_type_serialize_recursive(
1003 (void *) stream_class->common.event_context_field_type,
1004 context);
1005 if (ret) {
1006 BT_LOGW("Cannot serialize stream class's event context field type's metadata: "
1007 "ret=%d", ret);
1008 goto end;
1009 }
1010 g_string_append(context->string, ";");
1011 }
1012
1013 g_string_append(context->string, "\n};\n\n");
1014
1015 for (i = 0; i < stream_class->common.event_classes->len; i++) {
1016 struct bt_ctf_event_class *event_class =
1017 stream_class->common.event_classes->pdata[i];
1018
1019 ret = bt_ctf_event_class_serialize(event_class, context);
1020 if (ret) {
1021 BT_LOGW("Cannot serialize event class's metadata: "
1022 "event-class-addr=%p, event-class-name=\"%s\", "
1023 "event-class-id=%" PRId64,
1024 event_class,
1025 bt_ctf_event_class_get_name(event_class),
1026 bt_ctf_event_class_get_id(event_class));
1027 goto end;
1028 }
1029 }
1030
1031end:
e1e02a22 1032 bt_ctf_object_put_ref(packet_header_type);
3dca2276
PP
1033 context->current_indentation_level = 0;
1034 return ret;
1035}
1036
1037struct bt_ctf_trace *bt_ctf_stream_class_get_trace(
1038 struct bt_ctf_stream_class *stream_class)
1039{
e1e02a22 1040 return bt_ctf_object_get_ref(bt_ctf_stream_class_common_borrow_trace(
16ca5ff0 1041 BT_CTF_TO_COMMON(stream_class)));
3dca2276
PP
1042}
1043
1044const char *bt_ctf_stream_class_get_name(
1045 struct bt_ctf_stream_class *stream_class)
1046{
16ca5ff0 1047 return bt_ctf_stream_class_common_get_name(BT_CTF_TO_COMMON(stream_class));
3dca2276
PP
1048}
1049
1050int bt_ctf_stream_class_set_name(
1051 struct bt_ctf_stream_class *stream_class, const char *name)
1052{
16ca5ff0 1053 return bt_ctf_stream_class_common_set_name(BT_CTF_TO_COMMON(stream_class),
3dca2276
PP
1054 name);
1055}
1056
1057int64_t bt_ctf_stream_class_get_id(
1058 struct bt_ctf_stream_class *stream_class)
1059{
16ca5ff0 1060 return bt_ctf_stream_class_common_get_id(BT_CTF_TO_COMMON(stream_class));
3dca2276
PP
1061}
1062
1063int bt_ctf_stream_class_set_id(
1064 struct bt_ctf_stream_class *stream_class, uint64_t id)
1065{
16ca5ff0 1066 return bt_ctf_stream_class_common_set_id(BT_CTF_TO_COMMON(stream_class), id);
3dca2276
PP
1067}
1068
1069struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
1070 struct bt_ctf_stream_class *stream_class)
1071{
e1e02a22 1072 return bt_ctf_object_get_ref(
16ca5ff0
PP
1073 bt_ctf_stream_class_common_borrow_packet_context_field_type(
1074 BT_CTF_TO_COMMON(stream_class)));
3dca2276
PP
1075}
1076
1077int bt_ctf_stream_class_set_packet_context_type(
1078 struct bt_ctf_stream_class *stream_class,
1079 struct bt_ctf_field_type *packet_context_type)
1080{
16ca5ff0
PP
1081 return bt_ctf_stream_class_common_set_packet_context_field_type(
1082 BT_CTF_TO_COMMON(stream_class), (void *) packet_context_type);
3dca2276
PP
1083}
1084
1085struct bt_ctf_field_type *
1086bt_ctf_stream_class_get_event_header_type(
1087 struct bt_ctf_stream_class *stream_class)
1088{
e1e02a22 1089 return bt_ctf_object_get_ref(
16ca5ff0
PP
1090 bt_ctf_stream_class_common_borrow_event_header_field_type(
1091 BT_CTF_TO_COMMON(stream_class)));
3dca2276
PP
1092}
1093
1094int bt_ctf_stream_class_set_event_header_type(
1095 struct bt_ctf_stream_class *stream_class,
1096 struct bt_ctf_field_type *event_header_type)
1097{
16ca5ff0
PP
1098 return bt_ctf_stream_class_common_set_event_header_field_type(
1099 BT_CTF_TO_COMMON(stream_class), (void *) event_header_type);
3dca2276
PP
1100}
1101
1102struct bt_ctf_field_type *
1103bt_ctf_stream_class_get_event_context_type(
1104 struct bt_ctf_stream_class *stream_class)
1105{
e1e02a22 1106 return bt_ctf_object_get_ref(
16ca5ff0
PP
1107 bt_ctf_stream_class_common_borrow_event_context_field_type(
1108 BT_CTF_TO_COMMON(stream_class)));
3dca2276
PP
1109}
1110
1111int bt_ctf_stream_class_set_event_context_type(
1112 struct bt_ctf_stream_class *stream_class,
1113 struct bt_ctf_field_type *event_context_type)
1114{
16ca5ff0
PP
1115 return bt_ctf_stream_class_common_set_event_context_field_type(
1116 BT_CTF_TO_COMMON(stream_class), (void *) event_context_type);
3dca2276
PP
1117}
1118
1119int64_t bt_ctf_stream_class_get_event_class_count(
1120 struct bt_ctf_stream_class *stream_class)
1121{
16ca5ff0
PP
1122 return bt_ctf_stream_class_common_get_event_class_count(
1123 BT_CTF_TO_COMMON(stream_class));
3dca2276
PP
1124}
1125
1126struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_index(
1127 struct bt_ctf_stream_class *stream_class, uint64_t index)
1128{
e1e02a22 1129 return bt_ctf_object_get_ref(
16ca5ff0
PP
1130 bt_ctf_stream_class_common_borrow_event_class_by_index(
1131 BT_CTF_TO_COMMON(stream_class), index));
3dca2276
PP
1132}
1133
1134struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id(
1135 struct bt_ctf_stream_class *stream_class, uint64_t id)
1136{
e1e02a22 1137 return bt_ctf_object_get_ref(
16ca5ff0
PP
1138 bt_ctf_stream_class_common_borrow_event_class_by_id(
1139 BT_CTF_TO_COMMON(stream_class), id));
3dca2276
PP
1140}
1141
1142int bt_ctf_stream_class_add_event_class(
1143 struct bt_ctf_stream_class *stream_class,
1144 struct bt_ctf_event_class *event_class)
1145{
16ca5ff0
PP
1146 return bt_ctf_stream_class_common_add_event_class(
1147 BT_CTF_TO_COMMON(stream_class), BT_CTF_TO_COMMON(event_class),
1148 (bt_ctf_validation_flag_copy_field_type_func) bt_ctf_field_type_copy);
3dca2276 1149}
This page took 0.12644 seconds and 4 git commands to generate.