Commit | Line | Data |
---|---|---|
11b0cdc8 | 1 | /* |
3f043b05 | 2 | * stream-class.c |
11b0cdc8 | 3 | * |
d2dc44b6 | 4 | * Babeltrace CTF IR - Stream Class |
11b0cdc8 | 5 | * |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
11b0cdc8 JG |
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 | #include <babeltrace/ctf-writer/clock.h> | |
30 | #include <babeltrace/ctf-ir/clock-internal.h> | |
31 | #include <babeltrace/ctf-writer/event.h> | |
32 | #include <babeltrace/ctf-ir/event-internal.h> | |
33 | #include <babeltrace/ctf-ir/event-types-internal.h> | |
34 | #include <babeltrace/ctf-ir/event-fields-internal.h> | |
35 | #include <babeltrace/ctf-writer/stream.h> | |
36 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
26079216 | 37 | #include <babeltrace/ctf-ir/visitor-internal.h> |
11b0cdc8 | 38 | #include <babeltrace/ctf-writer/functor-internal.h> |
654c1444 | 39 | #include <babeltrace/ctf-ir/utils.h> |
11b0cdc8 JG |
40 | #include <babeltrace/compiler.h> |
41 | #include <babeltrace/align.h> | |
42 | ||
43 | static | |
44 | void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref); | |
45 | static | |
662e778c | 46 | int init_event_header(struct bt_ctf_stream_class *stream_class); |
11b0cdc8 | 47 | static |
662e778c | 48 | int init_packet_context(struct bt_ctf_stream_class *stream_class); |
11b0cdc8 JG |
49 | |
50 | struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name) | |
51 | { | |
12c8a1a3 | 52 | int ret; |
11b0cdc8 JG |
53 | struct bt_ctf_stream_class *stream_class = NULL; |
54 | ||
3ea33115 | 55 | if (name && bt_ctf_validate_identifier(name)) { |
11b0cdc8 JG |
56 | goto error; |
57 | } | |
58 | ||
59 | stream_class = g_new0(struct bt_ctf_stream_class, 1); | |
60 | if (!stream_class) { | |
61 | goto error; | |
62 | } | |
63 | ||
64 | stream_class->name = g_string_new(name); | |
65 | stream_class->event_classes = g_ptr_array_new_with_free_func( | |
66 | (GDestroyNotify)bt_ctf_event_class_put); | |
67 | if (!stream_class->event_classes) { | |
68 | goto error_destroy; | |
69 | } | |
70 | ||
662e778c JG |
71 | ret = init_event_header(stream_class); |
72 | if (ret) { | |
73 | goto error_destroy; | |
74 | } | |
75 | ||
76 | ret = init_packet_context(stream_class); | |
12c8a1a3 JG |
77 | if (ret) { |
78 | goto error_destroy; | |
79 | } | |
80 | ||
11b0cdc8 JG |
81 | bt_ctf_ref_init(&stream_class->ref_count); |
82 | return stream_class; | |
83 | ||
84 | error_destroy: | |
85 | bt_ctf_stream_class_destroy(&stream_class->ref_count); | |
86 | stream_class = NULL; | |
87 | error: | |
88 | return stream_class; | |
89 | } | |
90 | ||
142c5610 JG |
91 | struct bt_ctf_trace *bt_ctf_stream_class_get_trace( |
92 | struct bt_ctf_stream_class *stream_class) | |
93 | { | |
94 | struct bt_ctf_trace *trace = NULL; | |
95 | ||
96 | if (!stream_class) { | |
97 | goto end; | |
98 | } | |
99 | ||
100 | trace = stream_class->trace; | |
101 | if (trace) { | |
102 | bt_ctf_trace_get(trace); | |
103 | } | |
104 | end: | |
105 | return trace; | |
106 | } | |
107 | ||
69dc4535 JG |
108 | const char *bt_ctf_stream_class_get_name( |
109 | struct bt_ctf_stream_class *stream_class) | |
110 | { | |
111 | const char *name = NULL; | |
112 | ||
113 | if (!stream_class) { | |
114 | goto end; | |
115 | } | |
116 | ||
117 | name = stream_class->name->str; | |
118 | end: | |
119 | return name; | |
120 | } | |
121 | ||
3ea33115 JG |
122 | int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class, |
123 | const char *name) | |
124 | { | |
125 | int ret = 0; | |
126 | ||
127 | if (!stream_class || stream_class->frozen) { | |
128 | ret = -1; | |
129 | goto end; | |
130 | } | |
131 | ||
132 | g_string_assign(stream_class->name, name); | |
133 | end: | |
134 | return ret; | |
135 | } | |
136 | ||
2f100782 JG |
137 | struct bt_ctf_clock *bt_ctf_stream_class_get_clock( |
138 | struct bt_ctf_stream_class *stream_class) | |
139 | { | |
140 | struct bt_ctf_clock *clock = NULL; | |
141 | ||
142 | if (!stream_class || !stream_class->clock) { | |
143 | goto end; | |
144 | } | |
145 | ||
146 | clock = stream_class->clock; | |
147 | bt_ctf_clock_get(clock); | |
148 | end: | |
149 | return clock; | |
150 | } | |
151 | ||
11b0cdc8 JG |
152 | int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, |
153 | struct bt_ctf_clock *clock) | |
154 | { | |
155 | int ret = 0; | |
eee752e5 | 156 | struct bt_ctf_field_type *timestamp_field = NULL; |
11b0cdc8 JG |
157 | |
158 | if (!stream_class || !clock || stream_class->frozen) { | |
159 | ret = -1; | |
160 | goto end; | |
161 | } | |
162 | ||
eee752e5 JG |
163 | /* |
164 | * Look for a "timestamp" field in the stream class' event header type | |
165 | * and map the stream's clock to that field if no current mapping is | |
166 | * currently set. | |
167 | */ | |
168 | timestamp_field = bt_ctf_field_type_structure_get_field_type_by_name( | |
169 | stream_class->event_header_type, "timestamp"); | |
170 | if (timestamp_field) { | |
171 | struct bt_ctf_clock *mapped_clock; | |
172 | ||
173 | mapped_clock = bt_ctf_field_type_integer_get_mapped_clock( | |
174 | timestamp_field); | |
175 | if (mapped_clock) { | |
176 | bt_ctf_clock_put(mapped_clock); | |
177 | goto end; | |
178 | } | |
179 | ||
180 | ret = bt_ctf_field_type_integer_set_mapped_clock( | |
181 | timestamp_field, clock); | |
182 | if (ret) { | |
183 | goto end; | |
184 | } | |
185 | } | |
186 | ||
11b0cdc8 JG |
187 | if (stream_class->clock) { |
188 | bt_ctf_clock_put(stream_class->clock); | |
189 | } | |
190 | ||
191 | stream_class->clock = clock; | |
192 | bt_ctf_clock_get(clock); | |
193 | end: | |
eee752e5 JG |
194 | if (timestamp_field) { |
195 | bt_ctf_field_type_put(timestamp_field); | |
196 | } | |
11b0cdc8 JG |
197 | return ret; |
198 | } | |
199 | ||
2f100782 JG |
200 | int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class) |
201 | { | |
202 | int64_t ret; | |
203 | ||
204 | if (!stream_class || !stream_class->id_set) { | |
205 | ret = -1; | |
206 | goto end; | |
207 | } | |
208 | ||
209 | ret = (int64_t) stream_class->id; | |
210 | end: | |
211 | return ret; | |
212 | } | |
213 | ||
5ca83563 JG |
214 | BT_HIDDEN |
215 | int _bt_ctf_stream_class_set_id( | |
216 | struct bt_ctf_stream_class *stream_class, uint32_t id) | |
217 | { | |
218 | stream_class->id = id; | |
219 | stream_class->id_set = 1; | |
220 | return 0; | |
221 | } | |
222 | ||
29664b2a PP |
223 | struct event_class_set_stream_id_data { |
224 | uint32_t stream_id; | |
225 | int ret; | |
226 | }; | |
227 | ||
228 | static | |
229 | void event_class_set_stream_id(gpointer event_class, gpointer data) | |
230 | { | |
231 | struct event_class_set_stream_id_data *typed_data = data; | |
232 | ||
233 | typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class, | |
234 | typed_data->stream_id); | |
235 | } | |
236 | ||
237 | BT_HIDDEN | |
238 | int bt_ctf_stream_class_set_id_no_check( | |
239 | struct bt_ctf_stream_class *stream_class, uint32_t id) | |
2f100782 JG |
240 | { |
241 | int ret = 0; | |
29664b2a PP |
242 | struct event_class_set_stream_id_data data = |
243 | { .stream_id = id, .ret = 0 }; | |
2f100782 | 244 | |
29664b2a PP |
245 | /* |
246 | * Make sure all event classes have their "stream_id" attribute | |
247 | * set to this value. | |
248 | */ | |
249 | g_ptr_array_foreach(stream_class->event_classes, | |
250 | event_class_set_stream_id, &data); | |
251 | ret = data.ret; | |
252 | if (ret) { | |
2f100782 JG |
253 | goto end; |
254 | } | |
255 | ||
5ca83563 JG |
256 | ret = _bt_ctf_stream_class_set_id(stream_class, id); |
257 | if (ret) { | |
258 | goto end; | |
259 | } | |
2f100782 JG |
260 | end: |
261 | return ret; | |
262 | } | |
263 | ||
29664b2a PP |
264 | int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, |
265 | uint32_t id) | |
266 | { | |
267 | int ret = 0; | |
268 | ||
269 | if (!stream_class || stream_class->frozen) { | |
270 | ret = -1; | |
271 | goto end; | |
272 | } | |
273 | ||
274 | ret = bt_ctf_stream_class_set_id_no_check(stream_class, id); | |
275 | end: | |
276 | return ret; | |
277 | } | |
278 | ||
0d23acbe PP |
279 | static |
280 | void event_class_exists(gpointer element, gpointer query) | |
281 | { | |
282 | struct bt_ctf_event_class *event_class_a = element; | |
283 | struct search_query *search_query = query; | |
284 | struct bt_ctf_event_class *event_class_b = search_query->value; | |
285 | int64_t id_a, id_b; | |
286 | ||
287 | if (search_query->value == element) { | |
288 | search_query->found = 1; | |
289 | goto end; | |
290 | } | |
291 | ||
292 | /* | |
293 | * Two event classes cannot share the same name in a given | |
294 | * stream class. | |
295 | */ | |
296 | if (!strcmp(bt_ctf_event_class_get_name(event_class_a), | |
297 | bt_ctf_event_class_get_name(event_class_b))) { | |
298 | search_query->found = 1; | |
299 | goto end; | |
300 | } | |
301 | ||
302 | /* | |
303 | * Two event classes cannot share the same ID in a given | |
304 | * stream class. | |
305 | */ | |
306 | id_a = bt_ctf_event_class_get_id(event_class_a); | |
307 | id_b = bt_ctf_event_class_get_id(event_class_b); | |
308 | ||
309 | if (id_a < 0 || id_b < 0) { | |
310 | /* at least one ID is not set: will be automatically set later */ | |
311 | goto end; | |
312 | } | |
313 | ||
314 | if (id_a == id_b) { | |
315 | search_query->found = 1; | |
316 | goto end; | |
317 | } | |
318 | ||
319 | end: | |
320 | return; | |
321 | } | |
322 | ||
11b0cdc8 JG |
323 | int bt_ctf_stream_class_add_event_class( |
324 | struct bt_ctf_stream_class *stream_class, | |
325 | struct bt_ctf_event_class *event_class) | |
326 | { | |
327 | int ret = 0; | |
2f100782 | 328 | int64_t event_id; |
11b0cdc8 JG |
329 | |
330 | if (!stream_class || !event_class) { | |
331 | ret = -1; | |
332 | goto end; | |
333 | } | |
334 | ||
335 | /* Check for duplicate event classes */ | |
336 | struct search_query query = { .value = event_class, .found = 0 }; | |
0d23acbe PP |
337 | g_ptr_array_foreach(stream_class->event_classes, event_class_exists, |
338 | &query); | |
11b0cdc8 JG |
339 | if (query.found) { |
340 | ret = -1; | |
341 | goto end; | |
342 | } | |
343 | ||
26079216 JG |
344 | /* |
345 | * Resolve the event's sequence length and variant tags if the | |
346 | * stream is already associated with a trace. Otherwise, this | |
347 | * validation will be performed once the stream is registered | |
348 | * to a trace. | |
349 | */ | |
350 | if (stream_class->trace) { | |
351 | ret = bt_ctf_event_class_resolve_types(event_class, | |
352 | stream_class->trace, stream_class); | |
353 | if (ret) { | |
354 | goto end; | |
355 | } | |
356 | } | |
357 | ||
2f100782 JG |
358 | /* Only set an event id if none was explicitly set before */ |
359 | event_id = bt_ctf_event_class_get_id(event_class); | |
360 | if (event_id < 0) { | |
361 | if (bt_ctf_event_class_set_id(event_class, | |
362 | stream_class->next_event_id++)) { | |
363 | ret = -1; | |
364 | goto end; | |
365 | } | |
366 | } | |
367 | ||
368 | ret = bt_ctf_event_class_set_stream_class(event_class, stream_class); | |
369 | if (ret) { | |
11b0cdc8 JG |
370 | goto end; |
371 | } | |
372 | ||
29664b2a PP |
373 | ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id); |
374 | if (ret) { | |
375 | goto end; | |
376 | } | |
377 | ||
11b0cdc8 JG |
378 | bt_ctf_event_class_get(event_class); |
379 | g_ptr_array_add(stream_class->event_classes, event_class); | |
58203827 | 380 | bt_ctf_event_class_freeze(event_class); |
5ca83563 JG |
381 | |
382 | if (stream_class->byte_order) { | |
383 | /* | |
384 | * Only set native byte order if it has been initialized | |
385 | * when the stream class was added to a trace. | |
386 | * | |
387 | * If not set here, this will be set when the stream | |
388 | * classe will be added to a trace. | |
389 | */ | |
390 | bt_ctf_event_class_set_native_byte_order(event_class, | |
391 | stream_class->byte_order); | |
392 | } | |
11b0cdc8 JG |
393 | end: |
394 | return ret; | |
395 | } | |
396 | ||
074ee56d | 397 | int bt_ctf_stream_class_get_event_class_count( |
69dc4535 JG |
398 | struct bt_ctf_stream_class *stream_class) |
399 | { | |
074ee56d | 400 | int ret; |
69dc4535 JG |
401 | |
402 | if (!stream_class) { | |
403 | ret = -1; | |
404 | goto end; | |
405 | } | |
406 | ||
074ee56d | 407 | ret = (int) stream_class->event_classes->len; |
69dc4535 JG |
408 | end: |
409 | return ret; | |
410 | } | |
411 | ||
412 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class( | |
074ee56d | 413 | struct bt_ctf_stream_class *stream_class, int index) |
69dc4535 JG |
414 | { |
415 | struct bt_ctf_event_class *event_class = NULL; | |
416 | ||
074ee56d JG |
417 | if (!stream_class || index < 0 || |
418 | index >= stream_class->event_classes->len) { | |
69dc4535 JG |
419 | goto end; |
420 | } | |
421 | ||
422 | event_class = g_ptr_array_index(stream_class->event_classes, index); | |
423 | bt_ctf_event_class_get(event_class); | |
424 | end: | |
425 | return event_class; | |
426 | } | |
427 | ||
428 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name( | |
429 | struct bt_ctf_stream_class *stream_class, const char *name) | |
430 | { | |
431 | size_t i; | |
69dc4535 JG |
432 | struct bt_ctf_event_class *event_class = NULL; |
433 | ||
434 | if (!stream_class || !name) { | |
435 | goto end; | |
436 | } | |
437 | ||
69dc4535 | 438 | for (i = 0; i < stream_class->event_classes->len; i++) { |
b8248cc0 | 439 | struct bt_ctf_event_class *cur_event_class = |
69dc4535 | 440 | g_ptr_array_index(stream_class->event_classes, i); |
b8248cc0 PP |
441 | const char *cur_event_class_name = |
442 | bt_ctf_event_class_get_name(cur_event_class); | |
69dc4535 | 443 | |
b8248cc0 PP |
444 | if (!strcmp(name, cur_event_class_name)) { |
445 | event_class = cur_event_class; | |
69dc4535 JG |
446 | bt_ctf_event_class_get(event_class); |
447 | goto end; | |
448 | } | |
449 | } | |
450 | end: | |
451 | return event_class; | |
452 | } | |
453 | ||
0863f950 PP |
454 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id( |
455 | struct bt_ctf_stream_class *stream_class, uint32_t id) | |
456 | { | |
457 | size_t i; | |
458 | struct bt_ctf_event_class *event_class = NULL; | |
459 | ||
460 | if (!stream_class) { | |
461 | goto end; | |
462 | } | |
463 | ||
464 | for (i = 0; i < stream_class->event_classes->len; i++) { | |
465 | struct bt_ctf_event_class *current_event_class = | |
466 | g_ptr_array_index(stream_class->event_classes, i); | |
467 | ||
468 | if (bt_ctf_event_class_get_id(current_event_class) == id) { | |
469 | event_class = current_event_class; | |
470 | bt_ctf_event_class_get(event_class); | |
471 | goto end; | |
472 | } | |
473 | } | |
474 | end: | |
475 | return event_class; | |
476 | } | |
477 | ||
12c8a1a3 JG |
478 | struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type( |
479 | struct bt_ctf_stream_class *stream_class) | |
480 | { | |
481 | struct bt_ctf_field_type *ret = NULL; | |
482 | ||
483 | if (!stream_class) { | |
484 | goto end; | |
485 | } | |
486 | ||
487 | assert(stream_class->packet_context_type); | |
488 | bt_ctf_field_type_get(stream_class->packet_context_type); | |
489 | ret = stream_class->packet_context_type; | |
490 | end: | |
491 | return ret; | |
492 | } | |
493 | ||
494 | int bt_ctf_stream_class_set_packet_context_type( | |
495 | struct bt_ctf_stream_class *stream_class, | |
496 | struct bt_ctf_field_type *packet_context_type) | |
497 | { | |
498 | int ret = 0; | |
499 | ||
0a00bfa1 | 500 | if (!stream_class || !packet_context_type || stream_class->frozen) { |
12c8a1a3 JG |
501 | ret = -1; |
502 | goto end; | |
503 | } | |
504 | ||
505 | assert(stream_class->packet_context_type); | |
662e778c JG |
506 | if (stream_class->packet_context_type == packet_context_type) { |
507 | goto end; | |
508 | } | |
b34f4d90 | 509 | if (bt_ctf_field_type_get_type_id(packet_context_type) != |
12c8a1a3 JG |
510 | CTF_TYPE_STRUCT) { |
511 | /* A packet context must be a structure */ | |
512 | ret = -1; | |
513 | goto end; | |
514 | } | |
515 | ||
516 | bt_ctf_field_type_put(stream_class->packet_context_type); | |
517 | bt_ctf_field_type_get(packet_context_type); | |
518 | stream_class->packet_context_type = packet_context_type; | |
519 | end: | |
520 | return ret; | |
521 | } | |
522 | ||
662e778c JG |
523 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type( |
524 | struct bt_ctf_stream_class *stream_class) | |
525 | { | |
526 | struct bt_ctf_field_type *ret = NULL; | |
527 | ||
528 | if (!stream_class || !stream_class->event_header_type) { | |
529 | goto end; | |
530 | } | |
531 | ||
532 | assert(stream_class->event_header_type); | |
533 | bt_ctf_field_type_get(stream_class->event_header_type); | |
534 | ret = stream_class->event_header_type; | |
535 | end: | |
536 | return ret; | |
537 | } | |
538 | ||
539 | int bt_ctf_stream_class_set_event_header_type( | |
540 | struct bt_ctf_stream_class *stream_class, | |
541 | struct bt_ctf_field_type *event_header_type) | |
542 | { | |
543 | int ret = 0; | |
544 | ||
545 | if (!stream_class || !event_header_type || stream_class->frozen) { | |
546 | ret = -1; | |
547 | goto end; | |
548 | } | |
549 | ||
550 | assert(stream_class->event_header_type); | |
551 | if (stream_class->event_header_type == event_header_type) { | |
552 | goto end; | |
553 | } | |
554 | if (bt_ctf_field_type_get_type_id(event_header_type) != | |
555 | CTF_TYPE_STRUCT) { | |
556 | /* An event header must be a structure */ | |
557 | ret = -1; | |
558 | goto end; | |
559 | } | |
560 | ||
561 | bt_ctf_field_type_put(stream_class->event_header_type); | |
562 | bt_ctf_field_type_get(event_header_type); | |
563 | stream_class->event_header_type = event_header_type; | |
564 | end: | |
565 | return ret; | |
566 | } | |
567 | ||
af181248 JG |
568 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type( |
569 | struct bt_ctf_stream_class *stream_class) | |
570 | { | |
571 | struct bt_ctf_field_type *ret = NULL; | |
572 | ||
573 | if (!stream_class || !stream_class->event_context_type) { | |
574 | goto end; | |
575 | } | |
576 | ||
577 | assert(stream_class->event_context_type); | |
578 | bt_ctf_field_type_get(stream_class->event_context_type); | |
579 | ret = stream_class->event_context_type; | |
580 | end: | |
581 | return ret; | |
582 | } | |
583 | ||
584 | int bt_ctf_stream_class_set_event_context_type( | |
585 | struct bt_ctf_stream_class *stream_class, | |
586 | struct bt_ctf_field_type *event_context_type) | |
587 | { | |
588 | int ret = 0; | |
589 | ||
590 | if (!stream_class || !event_context_type || stream_class->frozen) { | |
591 | ret = -1; | |
592 | goto end; | |
593 | } | |
594 | ||
595 | if (bt_ctf_field_type_get_type_id(event_context_type) != | |
596 | CTF_TYPE_STRUCT) { | |
597 | /* A packet context must be a structure */ | |
598 | ret = -1; | |
599 | goto end; | |
600 | } | |
601 | ||
602 | bt_ctf_field_type_put(stream_class->event_context_type); | |
603 | bt_ctf_field_type_get(event_context_type); | |
604 | stream_class->event_context_type = event_context_type; | |
605 | end: | |
606 | return ret; | |
607 | } | |
608 | ||
11b0cdc8 JG |
609 | void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class) |
610 | { | |
611 | if (!stream_class) { | |
612 | return; | |
613 | } | |
614 | ||
615 | bt_ctf_ref_get(&stream_class->ref_count); | |
616 | } | |
617 | ||
618 | void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class) | |
619 | { | |
620 | if (!stream_class) { | |
621 | return; | |
622 | } | |
623 | ||
624 | bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy); | |
625 | } | |
626 | ||
627 | BT_HIDDEN | |
628 | void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class) | |
629 | { | |
630 | if (!stream_class) { | |
631 | return; | |
632 | } | |
633 | ||
634 | stream_class->frozen = 1; | |
662e778c | 635 | bt_ctf_field_type_freeze(stream_class->event_header_type); |
12c8a1a3 | 636 | bt_ctf_field_type_freeze(stream_class->packet_context_type); |
af181248 | 637 | bt_ctf_field_type_freeze(stream_class->event_context_type); |
11b0cdc8 | 638 | bt_ctf_clock_freeze(stream_class->clock); |
11b0cdc8 JG |
639 | } |
640 | ||
11b0cdc8 JG |
641 | BT_HIDDEN |
642 | int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class, | |
643 | enum bt_ctf_byte_order byte_order) | |
644 | { | |
5ca83563 | 645 | int i, ret = 0; |
c35a1669 | 646 | int internal_byte_order; |
11b0cdc8 | 647 | |
c35a1669 JG |
648 | /* Note that "NATIVE" means the trace's endianness, not the host's. */ |
649 | if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN || | |
5ca83563 | 650 | byte_order > BT_CTF_BYTE_ORDER_NETWORK) { |
c35a1669 JG |
651 | ret = -1; |
652 | goto end; | |
653 | } | |
654 | ||
655 | switch (byte_order) { | |
656 | case BT_CTF_BYTE_ORDER_NETWORK: | |
657 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: | |
658 | internal_byte_order = BIG_ENDIAN; | |
659 | break; | |
660 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: | |
661 | internal_byte_order = LITTLE_ENDIAN; | |
662 | break; | |
663 | default: | |
664 | ret = -1; | |
11b0cdc8 JG |
665 | goto end; |
666 | } | |
c35a1669 JG |
667 | |
668 | stream_class->byte_order = internal_byte_order; | |
5ca83563 JG |
669 | |
670 | /* Set native byte order to little or big endian */ | |
671 | bt_ctf_field_type_set_native_byte_order( | |
672 | stream_class->event_header_type, stream_class->byte_order); | |
673 | bt_ctf_field_type_set_native_byte_order( | |
674 | stream_class->packet_context_type, stream_class->byte_order); | |
675 | bt_ctf_field_type_set_native_byte_order( | |
676 | stream_class->event_context_type, stream_class->byte_order); | |
677 | ||
678 | /* Set all events' native byte order */ | |
679 | for (i = 0; i < stream_class->event_classes->len; i++) { | |
680 | bt_ctf_event_class_set_native_byte_order( | |
681 | g_ptr_array_index(stream_class->event_classes, i), | |
682 | stream_class->byte_order); | |
683 | bt_ctf_event_class_freeze( | |
684 | g_ptr_array_index(stream_class->event_classes, i)); | |
685 | } | |
11b0cdc8 JG |
686 | end: |
687 | return ret; | |
688 | } | |
689 | ||
690 | BT_HIDDEN | |
691 | int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class, | |
692 | struct metadata_context *context) | |
693 | { | |
2f100782 | 694 | int64_t ret = 0; |
11b0cdc8 JG |
695 | size_t i; |
696 | ||
697 | g_string_assign(context->field_name, ""); | |
698 | context->current_indentation_level = 1; | |
699 | if (!stream_class->id_set) { | |
700 | ret = -1; | |
701 | goto end; | |
702 | } | |
703 | ||
704 | g_string_append_printf(context->string, | |
705 | "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ", | |
706 | stream_class->id); | |
707 | ret = bt_ctf_field_type_serialize(stream_class->event_header_type, | |
708 | context); | |
709 | if (ret) { | |
710 | goto end; | |
711 | } | |
712 | ||
713 | g_string_append(context->string, ";\n\n\tpacket.context := "); | |
714 | ret = bt_ctf_field_type_serialize(stream_class->packet_context_type, | |
715 | context); | |
716 | if (ret) { | |
717 | goto end; | |
718 | } | |
719 | ||
720 | if (stream_class->event_context_type) { | |
721 | g_string_append(context->string, ";\n\n\tevent.context := "); | |
722 | ret = bt_ctf_field_type_serialize( | |
723 | stream_class->event_context_type, context); | |
724 | if (ret) { | |
725 | goto end; | |
726 | } | |
727 | } | |
728 | ||
729 | g_string_append(context->string, ";\n};\n\n"); | |
11b0cdc8 JG |
730 | for (i = 0; i < stream_class->event_classes->len; i++) { |
731 | struct bt_ctf_event_class *event_class = | |
732 | stream_class->event_classes->pdata[i]; | |
733 | ||
11b0cdc8 JG |
734 | ret = bt_ctf_event_class_serialize(event_class, context); |
735 | if (ret) { | |
736 | goto end; | |
737 | } | |
738 | } | |
739 | end: | |
740 | context->current_indentation_level = 0; | |
741 | return ret; | |
742 | } | |
743 | ||
d3814b54 JG |
744 | BT_HIDDEN |
745 | int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class *stream_class, | |
746 | struct bt_ctf_trace *trace) | |
747 | { | |
748 | int ret = 0; | |
749 | ||
750 | if (!stream_class) { | |
751 | ret = -1; | |
752 | goto end; | |
753 | } | |
754 | ||
755 | if (stream_class->trace && trace) { | |
756 | /* Already attached to a trace */ | |
757 | ret = -1; | |
758 | goto end; | |
759 | } | |
760 | ||
761 | stream_class->trace = trace; | |
762 | end: | |
763 | return ret; | |
764 | } | |
765 | ||
11b0cdc8 JG |
766 | static |
767 | void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref) | |
768 | { | |
769 | struct bt_ctf_stream_class *stream_class; | |
770 | ||
771 | if (!ref) { | |
772 | return; | |
773 | } | |
774 | ||
775 | stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count); | |
776 | bt_ctf_clock_put(stream_class->clock); | |
777 | ||
778 | if (stream_class->event_classes) { | |
2f100782 JG |
779 | size_t i; |
780 | ||
781 | /* Unregister this stream class from the event classes */ | |
782 | for (i = 0; i < stream_class->event_classes->len; i++) { | |
783 | struct bt_ctf_event_class *event_class = | |
784 | g_ptr_array_index(stream_class->event_classes, | |
785 | i); | |
786 | ||
787 | bt_ctf_event_class_set_stream_class(event_class, NULL); | |
788 | } | |
789 | ||
11b0cdc8 JG |
790 | g_ptr_array_free(stream_class->event_classes, TRUE); |
791 | } | |
792 | ||
793 | if (stream_class->name) { | |
794 | g_string_free(stream_class->name, TRUE); | |
795 | } | |
796 | ||
797 | bt_ctf_field_type_put(stream_class->event_header_type); | |
11b0cdc8 | 798 | bt_ctf_field_type_put(stream_class->packet_context_type); |
af181248 JG |
799 | if (stream_class->event_context_type) { |
800 | bt_ctf_field_type_put(stream_class->event_context_type); | |
801 | } | |
11b0cdc8 JG |
802 | g_free(stream_class); |
803 | } | |
804 | ||
805 | static | |
662e778c | 806 | int init_event_header(struct bt_ctf_stream_class *stream_class) |
11b0cdc8 JG |
807 | { |
808 | int ret = 0; | |
809 | struct bt_ctf_field_type *event_header_type = | |
810 | bt_ctf_field_type_structure_create(); | |
811 | struct bt_ctf_field_type *_uint32_t = | |
812 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); | |
813 | struct bt_ctf_field_type *_uint64_t = | |
814 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); | |
815 | ||
816 | if (!event_header_type) { | |
817 | ret = -1; | |
818 | goto end; | |
819 | } | |
820 | ||
11b0cdc8 JG |
821 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
822 | _uint32_t, "id"); | |
823 | if (ret) { | |
824 | goto end; | |
825 | } | |
826 | ||
827 | ret = bt_ctf_field_type_structure_add_field(event_header_type, | |
828 | _uint64_t, "timestamp"); | |
829 | if (ret) { | |
830 | goto end; | |
831 | } | |
832 | ||
662e778c JG |
833 | if (stream_class->event_header_type) { |
834 | bt_ctf_field_type_put(stream_class->event_header_type); | |
de876b7f | 835 | } |
662e778c | 836 | stream_class->event_header_type = event_header_type; |
11b0cdc8 JG |
837 | end: |
838 | if (ret) { | |
839 | bt_ctf_field_type_put(event_header_type); | |
840 | } | |
841 | ||
842 | bt_ctf_field_type_put(_uint32_t); | |
843 | bt_ctf_field_type_put(_uint64_t); | |
844 | return ret; | |
845 | } | |
846 | ||
847 | static | |
662e778c | 848 | int init_packet_context(struct bt_ctf_stream_class *stream_class) |
11b0cdc8 JG |
849 | { |
850 | int ret = 0; | |
851 | struct bt_ctf_field_type *packet_context_type = | |
852 | bt_ctf_field_type_structure_create(); | |
853 | struct bt_ctf_field_type *_uint64_t = | |
854 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); | |
855 | ||
856 | if (!packet_context_type) { | |
857 | ret = -1; | |
858 | goto end; | |
859 | } | |
860 | ||
861 | /* | |
862 | * We create a stream packet context as proposed in the CTF | |
863 | * specification. | |
864 | */ | |
11b0cdc8 JG |
865 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
866 | _uint64_t, "timestamp_begin"); | |
867 | if (ret) { | |
868 | goto end; | |
869 | } | |
870 | ||
871 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
872 | _uint64_t, "timestamp_end"); | |
873 | if (ret) { | |
874 | goto end; | |
875 | } | |
876 | ||
877 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
878 | _uint64_t, "content_size"); | |
879 | if (ret) { | |
880 | goto end; | |
881 | } | |
882 | ||
883 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
884 | _uint64_t, "packet_size"); | |
885 | if (ret) { | |
886 | goto end; | |
887 | } | |
888 | ||
889 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
890 | _uint64_t, "events_discarded"); | |
891 | if (ret) { | |
892 | goto end; | |
893 | } | |
894 | ||
662e778c JG |
895 | if (stream_class->packet_context_type) { |
896 | bt_ctf_field_type_put(stream_class->packet_context_type); | |
897 | } | |
11b0cdc8 | 898 | stream_class->packet_context_type = packet_context_type; |
11b0cdc8 JG |
899 | end: |
900 | if (ret) { | |
901 | bt_ctf_field_type_put(packet_context_type); | |
902 | goto end; | |
903 | } | |
904 | ||
905 | bt_ctf_field_type_put(_uint64_t); | |
906 | return ret; | |
907 | } |