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