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