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