Make API CTF-agnostic
[deliverable/babeltrace.git] / lib / ctf-ir / stream-class.c
1 /*
2 * stream-class.c
3 *
4 * Babeltrace CTF IR - Stream Class
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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 #define BT_LOG_TAG "STREAM-CLASS"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/ctf-ir/clock-class-internal.h>
34 #include <babeltrace/ctf-ir/event-class-internal.h>
35 #include <babeltrace/ctf-ir/field-types-internal.h>
36 #include <babeltrace/ctf-ir/fields-internal.h>
37 #include <babeltrace/ctf-ir/stream-class-internal.h>
38 #include <babeltrace/ctf-ir/utils.h>
39 #include <babeltrace/ctf-ir/utils-internal.h>
40 #include <babeltrace/ctf-ir/field-wrapper-internal.h>
41 #include <babeltrace/ctf-ir/resolve-field-path-internal.h>
42 #include <babeltrace/ref.h>
43 #include <babeltrace/compiler-internal.h>
44 #include <babeltrace/align-internal.h>
45 #include <babeltrace/endian-internal.h>
46 #include <babeltrace/assert-internal.h>
47 #include <babeltrace/property-internal.h>
48 #include <inttypes.h>
49 #include <stdint.h>
50 #include <stdbool.h>
51
52 #define BT_ASSERT_PRE_STREAM_CLASS_HOT(_sc) \
53 BT_ASSERT_PRE_HOT((_sc), "Stream class", ": %!+S", (_sc))
54
55 static
56 void destroy_stream_class(struct bt_object *obj)
57 {
58 struct bt_stream_class *stream_class = (void *) obj;
59
60 BT_LIB_LOGD("Destroying stream class: %!+S", stream_class);
61 BT_LOGD_STR("Putting default clock class.");
62 bt_put(stream_class->default_clock_class);
63
64 if (stream_class->event_classes) {
65 BT_LOGD_STR("Destroying event classes.");
66 g_ptr_array_free(stream_class->event_classes, TRUE);
67 }
68
69 if (stream_class->name.str) {
70 g_string_free(stream_class->name.str, TRUE);
71 }
72
73 BT_LOGD_STR("Putting event header field type.");
74 bt_put(stream_class->event_header_ft);
75 BT_LOGD_STR("Putting packet context field type.");
76 bt_put(stream_class->packet_context_ft);
77 BT_LOGD_STR("Putting event common context field type.");
78 bt_put(stream_class->event_common_context_ft);
79 bt_object_pool_finalize(&stream_class->event_header_field_pool);
80 bt_object_pool_finalize(&stream_class->packet_context_field_pool);
81 g_free(stream_class);
82 }
83
84 static
85 void free_field_wrapper(struct bt_field_wrapper *field_wrapper,
86 struct bt_stream_class *stream_class)
87 {
88 bt_field_wrapper_destroy((void *) field_wrapper);
89 }
90
91 BT_ASSERT_PRE_FUNC
92 static
93 bool stream_class_id_is_unique(struct bt_trace *trace, uint64_t id)
94 {
95 uint64_t i;
96 bool is_unique = true;
97
98 for (i = 0; i < trace->stream_classes->len; i++) {
99 struct bt_stream_class *sc =
100 trace->stream_classes->pdata[i];
101
102 if (sc->id == id) {
103 is_unique = false;
104 goto end;
105 }
106 }
107
108 end:
109 return is_unique;
110 }
111
112 static
113 struct bt_stream_class *create_stream_class_with_id(struct bt_trace *trace,
114 uint64_t id)
115 {
116 struct bt_stream_class *stream_class = NULL;
117 int ret;
118
119 BT_ASSERT(trace);
120 BT_ASSERT_PRE(stream_class_id_is_unique(trace, id),
121 "Duplicate stream class ID: %![trace-]+t, id=%" PRIu64,
122 trace, id);
123 BT_LIB_LOGD("Creating stream class object: %![trace-]+t, id=%" PRIu64,
124 trace, id);
125 stream_class = g_new0(struct bt_stream_class, 1);
126 if (!stream_class) {
127 BT_LOGE_STR("Failed to allocate one stream class.");
128 goto error;
129 }
130
131 bt_object_init_shared_with_parent(&stream_class->base,
132 destroy_stream_class);
133
134 stream_class->name.str = g_string_new(NULL);
135 if (!stream_class->name.str) {
136 BT_LOGE_STR("Failed to allocate a GString.");
137 ret = -1;
138 goto end;
139 }
140
141 stream_class->id = id;
142 stream_class->assigns_automatic_event_class_id = true;
143 stream_class->assigns_automatic_stream_id = true;
144 stream_class->event_classes = g_ptr_array_new_with_free_func(
145 (GDestroyNotify) bt_object_try_spec_release);
146 if (!stream_class->event_classes) {
147 BT_LOGE_STR("Failed to allocate a GPtrArray.");
148 goto error;
149 }
150
151 ret = bt_object_pool_initialize(&stream_class->event_header_field_pool,
152 (bt_object_pool_new_object_func) bt_field_wrapper_new,
153 (bt_object_pool_destroy_object_func) free_field_wrapper,
154 stream_class);
155 if (ret) {
156 BT_LOGE("Failed to initialize event header field pool: ret=%d",
157 ret);
158 goto error;
159 }
160
161 ret = bt_object_pool_initialize(&stream_class->packet_context_field_pool,
162 (bt_object_pool_new_object_func) bt_field_wrapper_new,
163 (bt_object_pool_destroy_object_func) free_field_wrapper,
164 stream_class);
165 if (ret) {
166 BT_LOGE("Failed to initialize packet context field pool: ret=%d",
167 ret);
168 goto error;
169 }
170
171 bt_object_set_parent(&stream_class->base, &trace->base);
172 g_ptr_array_add(trace->stream_classes, stream_class);
173 bt_trace_freeze(trace);
174 BT_LIB_LOGD("Created stream class object: %!+S", stream_class);
175 goto end;
176
177 error:
178 BT_PUT(stream_class);
179
180 end:
181 return stream_class;
182 }
183
184 struct bt_stream_class *bt_stream_class_create(struct bt_trace *trace)
185 {
186 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
187 BT_ASSERT_PRE(trace->assigns_automatic_stream_class_id,
188 "Trace does not automatically assigns stream class IDs: "
189 "%![sc-]+t", trace);
190 return create_stream_class_with_id(trace,
191 (uint64_t) trace->stream_classes->len);
192 }
193
194 struct bt_stream_class *bt_stream_class_create_with_id(
195 struct bt_trace *trace, uint64_t id)
196 {
197 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
198 BT_ASSERT_PRE(!trace->assigns_automatic_stream_class_id,
199 "Trace automatically assigns stream class IDs: "
200 "%![sc-]+t", trace);
201 return create_stream_class_with_id(trace, id);
202 }
203
204 struct bt_trace *bt_stream_class_borrow_trace(struct bt_stream_class *stream_class)
205 {
206 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
207 return bt_stream_class_borrow_trace_inline(stream_class);
208 }
209
210 const char *bt_stream_class_get_name(struct bt_stream_class *stream_class)
211 {
212 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
213 return stream_class->name.value;
214 }
215
216 int bt_stream_class_set_name(struct bt_stream_class *stream_class,
217 const char *name)
218 {
219 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
220 BT_ASSERT_PRE_NON_NULL(name, "Name");
221 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
222 g_string_assign(stream_class->name.str, name);
223 stream_class->name.value = stream_class->name.str->str;
224 BT_LIB_LOGV("Set stream class's name: %!+S", stream_class);
225 return 0;
226 }
227
228 uint64_t bt_stream_class_get_id(struct bt_stream_class *stream_class)
229 {
230 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
231 return stream_class->id;
232 }
233
234 uint64_t bt_stream_class_get_event_class_count(
235 struct bt_stream_class *stream_class)
236 {
237 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
238 return (uint64_t) stream_class->event_classes->len;
239 }
240
241 struct bt_event_class *bt_stream_class_borrow_event_class_by_index(
242 struct bt_stream_class *stream_class, uint64_t index)
243 {
244 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
245 BT_ASSERT_PRE_VALID_INDEX(index, stream_class->event_classes->len);
246 return g_ptr_array_index(stream_class->event_classes, index);
247 }
248
249 struct bt_event_class *bt_stream_class_borrow_event_class_by_id(
250 struct bt_stream_class *trace, uint64_t id)
251 {
252 struct bt_event_class *event_class = NULL;
253 uint64_t i;
254
255 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
256
257 for (i = 0; i < trace->event_classes->len; i++) {
258 struct bt_event_class *event_class_candidate =
259 g_ptr_array_index(trace->event_classes, i);
260
261 if (event_class_candidate->id == id) {
262 event_class = event_class_candidate;
263 goto end;
264 }
265 }
266
267 end:
268 return event_class;
269 }
270
271 struct bt_field_type *bt_stream_class_borrow_packet_context_field_type(
272 struct bt_stream_class *stream_class)
273 {
274 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
275 return stream_class->packet_context_ft;
276 }
277
278 int bt_stream_class_set_packet_context_field_type(
279 struct bt_stream_class *stream_class,
280 struct bt_field_type *field_type)
281 {
282 int ret;
283 struct bt_resolve_field_path_context resolve_ctx = {
284 .packet_header = NULL,
285 .packet_context = field_type,
286 .event_header = NULL,
287 .event_common_context = NULL,
288 .event_specific_context = NULL,
289 .event_payload = NULL,
290 };
291
292 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
293 BT_ASSERT_PRE_NON_NULL(field_type, "Field type");
294 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
295 BT_ASSERT_PRE(bt_field_type_get_type_id(field_type) ==
296 BT_FIELD_TYPE_ID_STRUCTURE,
297 "Packet context field type is not a structure field type: %!+F",
298 field_type);
299 resolve_ctx.packet_header =
300 bt_stream_class_borrow_trace_inline(stream_class)->packet_header_ft;
301 ret = bt_resolve_field_paths(field_type, &resolve_ctx);
302 if (ret) {
303 goto end;
304 }
305
306 bt_field_type_make_part_of_trace(field_type);
307 bt_put(stream_class->packet_context_ft);
308 stream_class->packet_context_ft = bt_get(field_type);
309 bt_field_type_freeze(field_type);
310 BT_LIB_LOGV("Set stream class's packet context field type: %!+S",
311 stream_class);
312
313 end:
314 return ret;
315 }
316
317 struct bt_field_type *bt_stream_class_borrow_event_header_field_type(
318 struct bt_stream_class *stream_class)
319 {
320 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
321 return stream_class->event_header_ft;
322 }
323
324 int bt_stream_class_set_event_header_field_type(
325 struct bt_stream_class *stream_class,
326 struct bt_field_type *field_type)
327 {
328 int ret;
329 struct bt_resolve_field_path_context resolve_ctx = {
330 .packet_header = NULL,
331 .packet_context = NULL,
332 .event_header = field_type,
333 .event_common_context = NULL,
334 .event_specific_context = NULL,
335 .event_payload = NULL,
336 };
337
338 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
339 BT_ASSERT_PRE_NON_NULL(field_type, "Field type");
340 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
341 BT_ASSERT_PRE(bt_field_type_get_type_id(field_type) ==
342 BT_FIELD_TYPE_ID_STRUCTURE,
343 "Event header field type is not a structure field type: %!+F",
344 field_type);
345 resolve_ctx.packet_header =
346 bt_stream_class_borrow_trace_inline(stream_class)->packet_header_ft;
347 resolve_ctx.packet_context = stream_class->packet_context_ft;
348 ret = bt_resolve_field_paths(field_type, &resolve_ctx);
349 if (ret) {
350 goto end;
351 }
352
353 bt_field_type_make_part_of_trace(field_type);
354 bt_put(stream_class->event_header_ft);
355 stream_class->event_header_ft = bt_get(field_type);
356 bt_field_type_freeze(field_type);
357 BT_LIB_LOGV("Set stream class's event header field type: %!+S",
358 stream_class);
359
360 end:
361 return ret;
362 }
363
364 struct bt_field_type *bt_stream_class_borrow_event_common_context_field_type(
365 struct bt_stream_class *stream_class)
366 {
367 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
368 return stream_class->event_common_context_ft;
369 }
370
371 int bt_stream_class_set_event_common_context_field_type(
372 struct bt_stream_class *stream_class,
373 struct bt_field_type *field_type)
374 {
375 int ret;
376 struct bt_resolve_field_path_context resolve_ctx = {
377 .packet_header = NULL,
378 .packet_context = NULL,
379 .event_header = NULL,
380 .event_common_context = field_type,
381 .event_specific_context = NULL,
382 .event_payload = NULL,
383 };
384
385 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
386 BT_ASSERT_PRE_NON_NULL(field_type, "Field type");
387 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
388 BT_ASSERT_PRE(bt_field_type_get_type_id(field_type) ==
389 BT_FIELD_TYPE_ID_STRUCTURE,
390 "Event common context field type is not a structure field type: %!+F",
391 field_type);
392 resolve_ctx.packet_header =
393 bt_stream_class_borrow_trace_inline(stream_class)->packet_header_ft;
394 resolve_ctx.packet_context = stream_class->packet_context_ft;
395 resolve_ctx.event_header = stream_class->event_header_ft;
396 ret = bt_resolve_field_paths(field_type, &resolve_ctx);
397 if (ret) {
398 goto end;
399 }
400
401 bt_field_type_make_part_of_trace(field_type);
402 bt_put(stream_class->event_common_context_ft);
403 stream_class->event_common_context_ft = bt_get(field_type);
404 bt_field_type_freeze(field_type);
405 BT_LIB_LOGV("Set stream class's event common context field type: %!+S",
406 stream_class);
407
408 end:
409 return ret;
410 }
411
412 BT_HIDDEN
413 void _bt_stream_class_freeze(struct bt_stream_class *stream_class)
414 {
415 /* The field types and default clock class are already frozen */
416 BT_ASSERT(stream_class);
417 BT_LIB_LOGD("Freezing stream class: %!+S", stream_class);
418 stream_class->frozen = true;
419 }
420
421 int bt_stream_class_set_default_clock_class(
422 struct bt_stream_class *stream_class,
423 struct bt_clock_class *clock_class)
424 {
425 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
426 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
427 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
428 bt_put(stream_class->default_clock_class);
429 stream_class->default_clock_class = bt_get(clock_class);
430 bt_clock_class_freeze(clock_class);
431 BT_LIB_LOGV("Set stream class's default clock class: %!+S",
432 stream_class);
433 return 0;
434 }
435
436 struct bt_clock_class *bt_stream_class_borrow_default_clock_class(
437 struct bt_stream_class *stream_class)
438 {
439 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
440 return stream_class->default_clock_class;
441 }
442
443 bt_bool bt_stream_class_assigns_automatic_event_class_id(
444 struct bt_stream_class *stream_class)
445 {
446 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
447 return (bt_bool) stream_class->assigns_automatic_event_class_id;
448 }
449
450 int bt_stream_class_set_assigns_automatic_event_class_id(
451 struct bt_stream_class *stream_class, bt_bool value)
452 {
453 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
454 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
455 stream_class->assigns_automatic_event_class_id = (bool) value;
456 BT_LIB_LOGV("Set stream class's automatic event class ID "
457 "assignment property: %!+S", stream_class);
458 return 0;
459 }
460
461 bt_bool bt_stream_class_assigns_automatic_stream_id(
462 struct bt_stream_class *stream_class)
463 {
464 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
465 return (bt_bool) stream_class->assigns_automatic_stream_id;
466 }
467
468 int bt_stream_class_set_assigns_automatic_stream_id(
469 struct bt_stream_class *stream_class, bt_bool value)
470 {
471 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
472 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
473 stream_class->assigns_automatic_stream_id = (bool) value;
474 BT_LIB_LOGV("Set stream class's automatic stream ID "
475 "assignment property: %!+S", stream_class);
476 return 0;
477 }
478
479 bt_bool bt_stream_class_packets_have_discarded_event_counter_snapshot(
480 struct bt_stream_class *stream_class)
481 {
482 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
483 return (bt_bool) stream_class->packets_have_discarded_event_counter_snapshot;
484 }
485
486 int bt_stream_class_set_packets_have_discarded_event_counter_snapshot(
487 struct bt_stream_class *stream_class, bt_bool value)
488 {
489 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
490 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
491 stream_class->packets_have_discarded_event_counter_snapshot =
492 (bool) value;
493 BT_LIB_LOGV("Set stream class's "
494 "\"packets have discarded event counter snapshot\" property: "
495 "%!+S", stream_class);
496 return 0;
497 }
498
499 bt_bool bt_stream_class_packets_have_packet_counter_snapshot(
500 struct bt_stream_class *stream_class)
501 {
502 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
503 return (bt_bool) stream_class->packets_have_packet_counter_snapshot;
504 }
505
506 int bt_stream_class_set_packets_have_packet_counter_snapshot(
507 struct bt_stream_class *stream_class, bt_bool value)
508 {
509 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
510 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
511 stream_class->packets_have_packet_counter_snapshot =
512 (bool) value;
513 BT_LIB_LOGV("Set stream class's "
514 "\"packets have packet counter snapshot\" property: "
515 "%!+S", stream_class);
516 return 0;
517 }
518
519 bt_bool bt_stream_class_packets_have_default_beginning_clock_value(
520 struct bt_stream_class *stream_class)
521 {
522 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
523 return (bt_bool) stream_class->packets_have_default_beginning_cv;
524 }
525
526 int bt_stream_class_set_packets_have_default_beginning_clock_value(
527 struct bt_stream_class *stream_class, bt_bool value)
528 {
529 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
530 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
531 BT_ASSERT_PRE(!value || stream_class->default_clock_class,
532 "Stream class does not have a default clock class: %!+S",
533 stream_class);
534 stream_class->packets_have_default_beginning_cv = (bool) value;
535 BT_LIB_LOGV("Set stream class's "
536 "\"packets have default beginning clock value\" property: "
537 "%!+S", stream_class);
538 return 0;
539 }
540
541 bt_bool bt_stream_class_packets_have_default_end_clock_value(
542 struct bt_stream_class *stream_class)
543 {
544 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
545 return (bt_bool) stream_class->packets_have_default_end_cv;
546 }
547
548 int bt_stream_class_set_packets_have_default_end_clock_value(
549 struct bt_stream_class *stream_class, bt_bool value)
550 {
551 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
552 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
553 BT_ASSERT_PRE(!value || stream_class->default_clock_class,
554 "Stream class does not have a default clock class: %!+S",
555 stream_class);
556 stream_class->packets_have_default_end_cv = (bool) value;
557 BT_LIB_LOGV("Set stream class's "
558 "\"packets have default end clock value\" property: "
559 "%!+S", stream_class);
560 return 0;
561 }
562
563 bt_bool bt_stream_class_default_clock_is_always_known(
564 struct bt_stream_class *stream_class)
565 {
566 /* BT_CLOCK_VALUE_STATUS_UNKNOWN is not supported as of 2.0 */
567 return BT_TRUE;
568 }
This page took 0.043331 seconds and 5 git commands to generate.