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