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