lib: remove "unknown clock snapshot" concept
[babeltrace.git] / 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 "STREAM-CLASS"
25 #include <babeltrace/lib-logging-internal.h>
26
27 #include <babeltrace/assert-pre-internal.h>
28 #include <babeltrace/trace-ir/clock-class-internal.h>
29 #include <babeltrace/trace-ir/event-class-internal.h>
30 #include <babeltrace/trace-ir/field-class-internal.h>
31 #include <babeltrace/trace-ir/field-internal.h>
32 #include <babeltrace/trace-ir/stream-class-internal.h>
33 #include <babeltrace/trace-ir/trace-const.h>
34 #include <babeltrace/trace-ir/trace-internal.h>
35 #include <babeltrace/trace-ir/utils-internal.h>
36 #include <babeltrace/trace-ir/field-wrapper-internal.h>
37 #include <babeltrace/trace-ir/resolve-field-path-internal.h>
38 #include <babeltrace/compiler-internal.h>
39 #include <babeltrace/align-internal.h>
40 #include <babeltrace/endian-internal.h>
41 #include <babeltrace/assert-internal.h>
42 #include <babeltrace/property-internal.h>
43 #include <inttypes.h>
44 #include <stdint.h>
45 #include <stdbool.h>
46
47 #define BT_ASSERT_PRE_STREAM_CLASS_HOT(_sc) \
48 BT_ASSERT_PRE_HOT((_sc), "Stream class", ": %!+S", (_sc))
49
50 static
51 void destroy_stream_class(struct bt_object *obj)
52 {
53 struct bt_stream_class *stream_class = (void *) obj;
54
55 BT_LIB_LOGD("Destroying stream class: %!+S", stream_class);
56 BT_LOGD_STR("Putting default clock class.");
57 BT_OBJECT_PUT_REF_AND_RESET(stream_class->default_clock_class);
58
59 if (stream_class->event_classes) {
60 BT_LOGD_STR("Destroying event classes.");
61 g_ptr_array_free(stream_class->event_classes, TRUE);
62 stream_class->event_classes = NULL;
63 }
64
65 if (stream_class->name.str) {
66 g_string_free(stream_class->name.str, TRUE);
67 stream_class->name.str = NULL;
68 stream_class->name.value = NULL;
69 }
70
71 BT_LOGD_STR("Putting packet context field class.");
72 BT_OBJECT_PUT_REF_AND_RESET(stream_class->packet_context_fc);
73 BT_LOGD_STR("Putting event common context field class.");
74 BT_OBJECT_PUT_REF_AND_RESET(stream_class->event_common_context_fc);
75 bt_object_pool_finalize(&stream_class->packet_context_field_pool);
76 g_free(stream_class);
77 }
78
79 static
80 void free_field_wrapper(struct bt_field_wrapper *field_wrapper,
81 struct bt_stream_class *stream_class)
82 {
83 bt_field_wrapper_destroy((void *) field_wrapper);
84 }
85
86 BT_ASSERT_PRE_FUNC
87 static
88 bool stream_class_id_is_unique(const struct bt_trace_class *tc, uint64_t id)
89 {
90 uint64_t i;
91 bool is_unique = true;
92
93 for (i = 0; i < tc->stream_classes->len; i++) {
94 const struct bt_stream_class *sc =
95 tc->stream_classes->pdata[i];
96
97 if (sc->id == id) {
98 is_unique = false;
99 goto end;
100 }
101 }
102
103 end:
104 return is_unique;
105 }
106
107 static
108 struct bt_stream_class *create_stream_class_with_id(
109 struct bt_trace_class *tc, uint64_t id)
110 {
111 struct bt_stream_class *stream_class = NULL;
112 int ret;
113
114 BT_ASSERT(tc);
115 BT_ASSERT_PRE(stream_class_id_is_unique(tc, id),
116 "Duplicate stream class ID: %![tc-]+T, id=%" PRIu64, tc, id);
117 BT_LIB_LOGD("Creating stream class object: %![tc-]+T, id=%" PRIu64,
118 tc, id);
119 stream_class = g_new0(struct bt_stream_class, 1);
120 if (!stream_class) {
121 BT_LOGE_STR("Failed to allocate one stream class.");
122 goto error;
123 }
124
125 bt_object_init_shared_with_parent(&stream_class->base,
126 destroy_stream_class);
127
128 stream_class->name.str = g_string_new(NULL);
129 if (!stream_class->name.str) {
130 BT_LOGE_STR("Failed to allocate a GString.");
131 ret = -1;
132 goto end;
133 }
134
135 stream_class->id = id;
136 stream_class->assigns_automatic_event_class_id = true;
137 stream_class->assigns_automatic_stream_id = true;
138 stream_class->event_classes = g_ptr_array_new_with_free_func(
139 (GDestroyNotify) bt_object_try_spec_release);
140 if (!stream_class->event_classes) {
141 BT_LOGE_STR("Failed to allocate a GPtrArray.");
142 goto error;
143 }
144
145 ret = bt_object_pool_initialize(&stream_class->packet_context_field_pool,
146 (bt_object_pool_new_object_func) bt_field_wrapper_new,
147 (bt_object_pool_destroy_object_func) free_field_wrapper,
148 stream_class);
149 if (ret) {
150 BT_LOGE("Failed to initialize packet context field pool: ret=%d",
151 ret);
152 goto error;
153 }
154
155 bt_object_set_parent(&stream_class->base, &tc->base);
156 g_ptr_array_add(tc->stream_classes, stream_class);
157 bt_trace_class_freeze(tc);
158 BT_LIB_LOGD("Created stream class object: %!+S", stream_class);
159 goto end;
160
161 error:
162 BT_OBJECT_PUT_REF_AND_RESET(stream_class);
163
164 end:
165 return stream_class;
166 }
167
168 struct bt_stream_class *bt_stream_class_create(struct bt_trace_class *tc)
169 {
170 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
171 BT_ASSERT_PRE(tc->assigns_automatic_stream_class_id,
172 "Trace class does not automatically assigns stream class IDs: "
173 "%![sc-]+T", tc);
174 return create_stream_class_with_id(tc,
175 (uint64_t) tc->stream_classes->len);
176 }
177
178 struct bt_stream_class *bt_stream_class_create_with_id(
179 struct bt_trace_class *tc, uint64_t id)
180 {
181 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
182 BT_ASSERT_PRE(!tc->assigns_automatic_stream_class_id,
183 "Trace class automatically assigns stream class IDs: "
184 "%![sc-]+T", tc);
185 return create_stream_class_with_id(tc, id);
186 }
187
188 struct bt_trace_class *bt_stream_class_borrow_trace_class(
189 struct bt_stream_class *stream_class)
190 {
191 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
192 return bt_stream_class_borrow_trace_class_inline(stream_class);
193 }
194
195 const struct bt_trace_class *bt_stream_class_borrow_trace_class_const(
196 const struct bt_stream_class *stream_class)
197 {
198 return bt_stream_class_borrow_trace_class((void *) stream_class);
199 }
200
201 const char *bt_stream_class_get_name(const struct bt_stream_class *stream_class)
202 {
203 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
204 return stream_class->name.value;
205 }
206
207 enum bt_stream_class_status bt_stream_class_set_name(
208 struct bt_stream_class *stream_class,
209 const char *name)
210 {
211 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
212 BT_ASSERT_PRE_NON_NULL(name, "Name");
213 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
214 g_string_assign(stream_class->name.str, name);
215 stream_class->name.value = stream_class->name.str->str;
216 BT_LIB_LOGV("Set stream class's name: %!+S", stream_class);
217 return BT_STREAM_CLASS_STATUS_OK;
218 }
219
220 uint64_t bt_stream_class_get_id(const struct bt_stream_class *stream_class)
221 {
222 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
223 return stream_class->id;
224 }
225
226 uint64_t bt_stream_class_get_event_class_count(
227 const struct bt_stream_class *stream_class)
228 {
229 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
230 return (uint64_t) stream_class->event_classes->len;
231 }
232
233 struct bt_event_class *bt_stream_class_borrow_event_class_by_index(
234 struct bt_stream_class *stream_class, uint64_t index)
235 {
236 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
237 BT_ASSERT_PRE_VALID_INDEX(index, stream_class->event_classes->len);
238 return g_ptr_array_index(stream_class->event_classes, index);
239 }
240
241 const struct bt_event_class *
242 bt_stream_class_borrow_event_class_by_index_const(
243 const struct bt_stream_class *stream_class, uint64_t index)
244 {
245 return bt_stream_class_borrow_event_class_by_index(
246 (void *) stream_class, index);
247 }
248
249 struct bt_event_class *bt_stream_class_borrow_event_class_by_id(
250 struct bt_stream_class *stream_class, uint64_t id)
251 {
252 struct bt_event_class *event_class = NULL;
253 uint64_t i;
254
255 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
256
257 for (i = 0; i < stream_class->event_classes->len; i++) {
258 struct bt_event_class *event_class_candidate =
259 g_ptr_array_index(stream_class->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 const struct bt_event_class *
272 bt_stream_class_borrow_event_class_by_id_const(
273 const struct bt_stream_class *stream_class, uint64_t id)
274 {
275 return bt_stream_class_borrow_event_class_by_id(
276 (void *) stream_class, id);
277 }
278
279 const struct bt_field_class *
280 bt_stream_class_borrow_packet_context_field_class_const(
281 const struct bt_stream_class *stream_class)
282 {
283 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
284 return stream_class->packet_context_fc;
285 }
286
287 struct bt_field_class *
288 bt_stream_class_borrow_packet_context_field_class(
289 struct bt_stream_class *stream_class)
290 {
291 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
292 return stream_class->packet_context_fc;
293 }
294
295 enum bt_stream_class_status bt_stream_class_set_packet_context_field_class(
296 struct bt_stream_class *stream_class,
297 struct bt_field_class *field_class)
298 {
299 int ret;
300 struct bt_resolve_field_path_context resolve_ctx = {
301 .packet_context = field_class,
302 .event_common_context = NULL,
303 .event_specific_context = NULL,
304 .event_payload = NULL,
305 };
306
307 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
308 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
309 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
310 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
311 BT_FIELD_CLASS_TYPE_STRUCTURE,
312 "Packet context field class is not a structure field class: %!+F",
313 field_class);
314 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
315 if (ret) {
316 /*
317 * This is the only reason for which
318 * bt_resolve_field_paths() can fail: anything else
319 * would be because a precondition is not satisfied.
320 */
321 ret = BT_STREAM_CLASS_STATUS_NOMEM;
322 goto end;
323 }
324
325 bt_field_class_make_part_of_trace_class(field_class);
326 bt_object_put_ref(stream_class->packet_context_fc);
327 stream_class->packet_context_fc = field_class;
328 bt_object_get_no_null_check(stream_class->packet_context_fc);
329 bt_field_class_freeze(field_class);
330 BT_LIB_LOGV("Set stream class's packet context field class: %!+S",
331 stream_class);
332
333 end:
334 return ret;
335 }
336
337 const struct bt_field_class *
338 bt_stream_class_borrow_event_common_context_field_class_const(
339 const struct bt_stream_class *stream_class)
340 {
341 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
342 return stream_class->event_common_context_fc;
343 }
344
345 struct bt_field_class *
346 bt_stream_class_borrow_event_common_context_field_class(
347 struct bt_stream_class *stream_class)
348 {
349 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
350 return stream_class->event_common_context_fc;
351 }
352
353 enum bt_stream_class_status
354 bt_stream_class_set_event_common_context_field_class(
355 struct bt_stream_class *stream_class,
356 struct bt_field_class *field_class)
357 {
358 int ret;
359 struct bt_resolve_field_path_context resolve_ctx = {
360 .packet_context = NULL,
361 .event_common_context = field_class,
362 .event_specific_context = NULL,
363 .event_payload = NULL,
364 };
365
366 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
367 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
368 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
369 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
370 BT_FIELD_CLASS_TYPE_STRUCTURE,
371 "Event common context field class is not a structure field class: %!+F",
372 field_class);
373 resolve_ctx.packet_context = stream_class->packet_context_fc;
374 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
375 if (ret) {
376 /*
377 * This is the only reason for which
378 * bt_resolve_field_paths() can fail: anything else
379 * would be because a precondition is not satisfied.
380 */
381 ret = BT_STREAM_CLASS_STATUS_NOMEM;
382 goto end;
383 }
384
385 bt_field_class_make_part_of_trace_class(field_class);
386 bt_object_put_ref(stream_class->event_common_context_fc);
387 stream_class->event_common_context_fc = field_class;
388 bt_object_get_no_null_check(stream_class->event_common_context_fc);
389 bt_field_class_freeze(field_class);
390 BT_LIB_LOGV("Set stream class's event common context field class: %!+S",
391 stream_class);
392
393 end:
394 return ret;
395 }
396
397 BT_HIDDEN
398 void _bt_stream_class_freeze(const struct bt_stream_class *stream_class)
399 {
400 /* The field classes and default clock class are already frozen */
401 BT_ASSERT(stream_class);
402 BT_LIB_LOGD("Freezing stream class: %!+S", stream_class);
403 ((struct bt_stream_class *) stream_class)->frozen = true;
404 }
405
406 enum bt_stream_class_status bt_stream_class_set_default_clock_class(
407 struct bt_stream_class *stream_class,
408 struct bt_clock_class *clock_class)
409 {
410 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
411 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
412 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
413 bt_object_put_ref(stream_class->default_clock_class);
414 stream_class->default_clock_class = clock_class;
415 bt_object_get_no_null_check(stream_class->default_clock_class);
416 bt_clock_class_freeze(clock_class);
417 BT_LIB_LOGV("Set stream class's default clock class: %!+S",
418 stream_class);
419 return BT_STREAM_CLASS_STATUS_OK;
420 }
421
422 struct bt_clock_class *bt_stream_class_borrow_default_clock_class(
423 struct bt_stream_class *stream_class)
424 {
425 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
426 return stream_class->default_clock_class;
427 }
428
429 const struct bt_clock_class *bt_stream_class_borrow_default_clock_class_const(
430 const struct bt_stream_class *stream_class)
431 {
432 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
433 return stream_class->default_clock_class;
434 }
435
436 bt_bool bt_stream_class_assigns_automatic_event_class_id(
437 const struct bt_stream_class *stream_class)
438 {
439 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
440 return (bt_bool) stream_class->assigns_automatic_event_class_id;
441 }
442
443 void bt_stream_class_set_assigns_automatic_event_class_id(
444 struct bt_stream_class *stream_class,
445 bt_bool value)
446 {
447 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
448 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
449 stream_class->assigns_automatic_event_class_id = (bool) value;
450 BT_LIB_LOGV("Set stream class's automatic event class ID "
451 "assignment property: %!+S", stream_class);
452 }
453
454 bt_bool bt_stream_class_assigns_automatic_stream_id(
455 const struct bt_stream_class *stream_class)
456 {
457 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
458 return (bt_bool) stream_class->assigns_automatic_stream_id;
459 }
460
461 void bt_stream_class_set_assigns_automatic_stream_id(
462 struct bt_stream_class *stream_class,
463 bt_bool value)
464 {
465 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
466 BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class);
467 stream_class->assigns_automatic_stream_id = (bool) value;
468 BT_LIB_LOGV("Set stream class's automatic stream ID "
469 "assignment property: %!+S", stream_class);
470 }
471
472 void bt_stream_class_get_ref(const struct bt_stream_class *stream_class)
473 {
474 bt_object_get_ref(stream_class);
475 }
476
477 void bt_stream_class_put_ref(const struct bt_stream_class *stream_class)
478 {
479 bt_object_put_ref(stream_class);
480 }
This page took 0.039021 seconds and 5 git commands to generate.