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