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