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