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> | |
ac0c6bdd PP |
30 | #include <babeltrace/ctf-writer/clock-internal.h> |
31 | #include <babeltrace/ctf-ir/clock-class-internal.h> | |
11b0cdc8 | 32 | #include <babeltrace/ctf-writer/event.h> |
272df73e | 33 | #include <babeltrace/ctf-ir/event-class-internal.h> |
11b0cdc8 | 34 | #include <babeltrace/ctf-ir/event-internal.h> |
2e33ac5a PP |
35 | #include <babeltrace/ctf-ir/field-types-internal.h> |
36 | #include <babeltrace/ctf-ir/fields-internal.h> | |
11b0cdc8 JG |
37 | #include <babeltrace/ctf-writer/stream.h> |
38 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
09840de5 | 39 | #include <babeltrace/ctf-ir/validation-internal.h> |
8bf65fbd | 40 | #include <babeltrace/ctf-ir/visitor-internal.h> |
11b0cdc8 | 41 | #include <babeltrace/ctf-writer/functor-internal.h> |
654c1444 | 42 | #include <babeltrace/ctf-ir/utils.h> |
83509119 | 43 | #include <babeltrace/ref.h> |
3d9990ac PP |
44 | #include <babeltrace/compiler-internal.h> |
45 | #include <babeltrace/align-internal.h> | |
46 | #include <babeltrace/endian-internal.h> | |
dc3fffef | 47 | #include <inttypes.h> |
11b0cdc8 JG |
48 | |
49 | static | |
83509119 | 50 | void bt_ctf_stream_class_destroy(struct bt_object *obj); |
11b0cdc8 | 51 | static |
662e778c | 52 | int init_event_header(struct bt_ctf_stream_class *stream_class); |
11b0cdc8 | 53 | static |
662e778c | 54 | int init_packet_context(struct bt_ctf_stream_class *stream_class); |
11b0cdc8 JG |
55 | |
56 | struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name) | |
57 | { | |
12c8a1a3 | 58 | int ret; |
11b0cdc8 JG |
59 | struct bt_ctf_stream_class *stream_class = NULL; |
60 | ||
3ea33115 | 61 | if (name && bt_ctf_validate_identifier(name)) { |
11b0cdc8 JG |
62 | goto error; |
63 | } | |
64 | ||
65 | stream_class = g_new0(struct bt_ctf_stream_class, 1); | |
66 | if (!stream_class) { | |
67 | goto error; | |
68 | } | |
69 | ||
70 | stream_class->name = g_string_new(name); | |
71 | stream_class->event_classes = g_ptr_array_new_with_free_func( | |
e6a8e8e4 | 72 | (GDestroyNotify) bt_object_release); |
11b0cdc8 | 73 | if (!stream_class->event_classes) { |
83509119 | 74 | goto error; |
11b0cdc8 JG |
75 | } |
76 | ||
0b9ce69f JG |
77 | stream_class->event_classes_ht = g_hash_table_new_full(g_int64_hash, |
78 | g_int64_equal, g_free, NULL); | |
79 | ||
662e778c JG |
80 | ret = init_event_header(stream_class); |
81 | if (ret) { | |
83509119 | 82 | goto error; |
662e778c JG |
83 | } |
84 | ||
85 | ret = init_packet_context(stream_class); | |
12c8a1a3 | 86 | if (ret) { |
83509119 | 87 | goto error; |
12c8a1a3 JG |
88 | } |
89 | ||
83509119 | 90 | bt_object_init(stream_class, bt_ctf_stream_class_destroy); |
11b0cdc8 JG |
91 | return stream_class; |
92 | ||
11b0cdc8 | 93 | error: |
83509119 | 94 | BT_PUT(stream_class); |
11b0cdc8 JG |
95 | return stream_class; |
96 | } | |
97 | ||
142c5610 JG |
98 | struct bt_ctf_trace *bt_ctf_stream_class_get_trace( |
99 | struct bt_ctf_stream_class *stream_class) | |
100 | { | |
dc3fffef PP |
101 | return stream_class ? |
102 | bt_get(bt_ctf_stream_class_borrow_trace(stream_class)) : | |
103 | NULL; | |
142c5610 JG |
104 | } |
105 | ||
69dc4535 JG |
106 | const char *bt_ctf_stream_class_get_name( |
107 | struct bt_ctf_stream_class *stream_class) | |
108 | { | |
109 | const char *name = NULL; | |
110 | ||
111 | if (!stream_class) { | |
112 | goto end; | |
113 | } | |
114 | ||
115 | name = stream_class->name->str; | |
116 | end: | |
117 | return name; | |
118 | } | |
119 | ||
3ea33115 JG |
120 | int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class, |
121 | const char *name) | |
122 | { | |
123 | int ret = 0; | |
124 | ||
125 | if (!stream_class || stream_class->frozen) { | |
126 | ret = -1; | |
127 | goto end; | |
128 | } | |
129 | ||
130 | g_string_assign(stream_class->name, name); | |
131 | end: | |
132 | return ret; | |
133 | } | |
134 | ||
2f100782 JG |
135 | struct bt_ctf_clock *bt_ctf_stream_class_get_clock( |
136 | struct bt_ctf_stream_class *stream_class) | |
137 | { | |
138 | struct bt_ctf_clock *clock = NULL; | |
139 | ||
140 | if (!stream_class || !stream_class->clock) { | |
141 | goto end; | |
142 | } | |
143 | ||
ac0c6bdd | 144 | clock = bt_get(stream_class->clock); |
2f100782 JG |
145 | end: |
146 | return clock; | |
147 | } | |
148 | ||
11b0cdc8 JG |
149 | int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, |
150 | struct bt_ctf_clock *clock) | |
151 | { | |
152 | int ret = 0; | |
eee752e5 | 153 | struct bt_ctf_field_type *timestamp_field = NULL; |
11b0cdc8 | 154 | |
ac0c6bdd | 155 | if (!stream_class || !clock || stream_class->frozen) { |
11b0cdc8 JG |
156 | ret = -1; |
157 | goto end; | |
158 | } | |
159 | ||
eee752e5 | 160 | /* |
ac0c6bdd PP |
161 | * Look for a "timestamp" integer field type in the stream |
162 | * class's event header field type and map the stream class's | |
163 | * clock's class to that field type if there's no current | |
164 | * mapping. | |
eee752e5 JG |
165 | */ |
166 | timestamp_field = bt_ctf_field_type_structure_get_field_type_by_name( | |
167 | stream_class->event_header_type, "timestamp"); | |
168 | if (timestamp_field) { | |
ac0c6bdd PP |
169 | struct bt_ctf_clock_class *mapped_clock_class = |
170 | bt_ctf_field_type_integer_get_mapped_clock_class( | |
171 | timestamp_field); | |
172 | ||
173 | if (!mapped_clock_class) { | |
174 | ret = bt_ctf_field_type_integer_set_mapped_clock_class( | |
175 | timestamp_field, clock->clock_class); | |
176 | if (ret) { | |
177 | goto end; | |
178 | } | |
eee752e5 JG |
179 | } |
180 | ||
ac0c6bdd | 181 | BT_PUT(mapped_clock_class); |
eee752e5 JG |
182 | } |
183 | ||
ac0c6bdd PP |
184 | /* Replace the current clock of this stream class. */ |
185 | bt_put(stream_class->clock); | |
186 | stream_class->clock = bt_get(clock); | |
11b0cdc8 | 187 | |
11b0cdc8 | 188 | end: |
ac0c6bdd | 189 | bt_put(timestamp_field); |
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; | |
0b9ce69f | 321 | int64_t *event_id = NULL; |
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 | ||
0b9ce69f JG |
339 | event_id = g_new(int64_t, 1); |
340 | if (!event_id) { | |
341 | ret = -1; | |
342 | goto end; | |
343 | } | |
344 | ||
11b0cdc8 JG |
345 | /* Check for duplicate event classes */ |
346 | struct search_query query = { .value = event_class, .found = 0 }; | |
0d23acbe PP |
347 | g_ptr_array_foreach(stream_class->event_classes, event_class_exists, |
348 | &query); | |
11b0cdc8 JG |
349 | if (query.found) { |
350 | ret = -1; | |
351 | goto end; | |
352 | } | |
353 | ||
09840de5 | 354 | old_stream_class = bt_ctf_event_class_get_stream_class(event_class); |
e6a8e8e4 JG |
355 | if (old_stream_class) { |
356 | /* Event class is already associated to a stream class. */ | |
357 | ret = -1; | |
358 | goto end; | |
359 | } | |
360 | ||
09840de5 | 361 | trace = bt_ctf_stream_class_get_trace(stream_class); |
e6a8e8e4 | 362 | if (trace) { |
09840de5 PP |
363 | /* |
364 | * If the stream class is associated with a trace, then | |
365 | * both those objects are frozen. Also, this event class | |
366 | * is about to be frozen. | |
367 | * | |
368 | * Therefore the event class must be validated here. | |
369 | * The trace and stream class should be valid at this | |
370 | * point. | |
371 | */ | |
372 | assert(trace->valid); | |
373 | assert(stream_class->valid); | |
374 | packet_header_type = | |
375 | bt_ctf_trace_get_packet_header_type(trace); | |
376 | packet_context_type = | |
377 | bt_ctf_stream_class_get_packet_context_type( | |
378 | stream_class); | |
379 | event_header_type = | |
380 | bt_ctf_stream_class_get_event_header_type(stream_class); | |
381 | stream_event_ctx_type = | |
382 | bt_ctf_stream_class_get_event_context_type( | |
383 | stream_class); | |
384 | event_context_type = | |
385 | bt_ctf_event_class_get_context_type(event_class); | |
386 | event_payload_type = | |
387 | bt_ctf_event_class_get_payload_type(event_class); | |
388 | ret = bt_ctf_validate_class_types( | |
389 | trace->environment, packet_header_type, | |
390 | packet_context_type, event_header_type, | |
391 | stream_event_ctx_type, event_context_type, | |
392 | event_payload_type, trace->valid, | |
393 | stream_class->valid, event_class->valid, | |
394 | &validation_output, validation_flags); | |
395 | BT_PUT(packet_header_type); | |
396 | BT_PUT(packet_context_type); | |
397 | BT_PUT(event_header_type); | |
398 | BT_PUT(stream_event_ctx_type); | |
399 | BT_PUT(event_context_type); | |
400 | BT_PUT(event_payload_type); | |
401 | ||
26079216 | 402 | if (ret) { |
09840de5 PP |
403 | /* |
404 | * This means something went wrong during the | |
405 | * validation process, not that the objects are | |
406 | * invalid. | |
407 | */ | |
408 | goto end; | |
409 | } | |
410 | ||
411 | if ((validation_output.valid_flags & validation_flags) != | |
412 | validation_flags) { | |
413 | /* Invalid event class */ | |
414 | ret = -1; | |
26079216 JG |
415 | goto end; |
416 | } | |
417 | } | |
418 | ||
09840de5 | 419 | /* Only set an event ID if none was explicitly set before */ |
0b9ce69f | 420 | *event_id = bt_ctf_event_class_get_id(event_class); |
24626e8b | 421 | if (*event_id < 0) { |
2f100782 JG |
422 | if (bt_ctf_event_class_set_id(event_class, |
423 | stream_class->next_event_id++)) { | |
424 | ret = -1; | |
425 | goto end; | |
426 | } | |
0b9ce69f | 427 | *event_id = stream_class->next_event_id; |
2f100782 JG |
428 | } |
429 | ||
29664b2a PP |
430 | ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id); |
431 | if (ret) { | |
432 | goto end; | |
433 | } | |
434 | ||
e6a8e8e4 | 435 | bt_object_set_parent(event_class, stream_class); |
09840de5 PP |
436 | |
437 | if (trace) { | |
438 | /* | |
439 | * At this point we know that the function will be | |
440 | * successful. Therefore we can replace the event | |
441 | * class's field types with what's in the validation | |
442 | * output structure and mark this event class as valid. | |
443 | */ | |
444 | bt_ctf_validation_replace_types(NULL, NULL, event_class, | |
445 | &validation_output, validation_flags); | |
446 | event_class->valid = 1; | |
447 | ||
448 | /* | |
449 | * Put what was not moved in | |
450 | * bt_ctf_validation_replace_types(). | |
451 | */ | |
452 | bt_ctf_validation_output_put_types(&validation_output); | |
453 | } | |
454 | ||
455 | /* Add to the event classes of the stream class */ | |
11b0cdc8 | 456 | g_ptr_array_add(stream_class->event_classes, event_class); |
0b9ce69f JG |
457 | g_hash_table_insert(stream_class->event_classes_ht, event_id, |
458 | event_class); | |
459 | event_id = NULL; | |
09840de5 PP |
460 | |
461 | /* Freeze the event class */ | |
58203827 | 462 | bt_ctf_event_class_freeze(event_class); |
5ca83563 | 463 | |
9b888ff3 JG |
464 | /* Notifiy listeners of the trace's schema modification. */ |
465 | if (trace) { | |
d9a13d86 PP |
466 | struct bt_ctf_object obj = { .object = event_class, |
467 | .type = BT_CTF_OBJECT_TYPE_EVENT_CLASS }; | |
9b888ff3 | 468 | |
d9a13d86 | 469 | (void) bt_ctf_trace_object_modification(&obj, trace); |
9b888ff3 | 470 | } |
11b0cdc8 | 471 | end: |
e6a8e8e4 JG |
472 | BT_PUT(trace); |
473 | BT_PUT(old_stream_class); | |
09840de5 PP |
474 | bt_ctf_validation_output_put_types(&validation_output); |
475 | assert(!packet_header_type); | |
476 | assert(!packet_context_type); | |
477 | assert(!event_header_type); | |
478 | assert(!stream_event_ctx_type); | |
479 | assert(!event_context_type); | |
480 | assert(!event_payload_type); | |
0b9ce69f | 481 | g_free(event_id); |
09840de5 | 482 | |
11b0cdc8 JG |
483 | return ret; |
484 | } | |
485 | ||
074ee56d | 486 | int bt_ctf_stream_class_get_event_class_count( |
69dc4535 JG |
487 | struct bt_ctf_stream_class *stream_class) |
488 | { | |
074ee56d | 489 | int ret; |
69dc4535 JG |
490 | |
491 | if (!stream_class) { | |
492 | ret = -1; | |
493 | goto end; | |
494 | } | |
495 | ||
074ee56d | 496 | ret = (int) stream_class->event_classes->len; |
69dc4535 JG |
497 | end: |
498 | return ret; | |
499 | } | |
500 | ||
501 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class( | |
074ee56d | 502 | struct bt_ctf_stream_class *stream_class, int index) |
69dc4535 JG |
503 | { |
504 | struct bt_ctf_event_class *event_class = NULL; | |
505 | ||
074ee56d JG |
506 | if (!stream_class || index < 0 || |
507 | index >= stream_class->event_classes->len) { | |
69dc4535 JG |
508 | goto end; |
509 | } | |
510 | ||
511 | event_class = g_ptr_array_index(stream_class->event_classes, index); | |
83509119 | 512 | bt_get(event_class); |
69dc4535 JG |
513 | end: |
514 | return event_class; | |
515 | } | |
516 | ||
517 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name( | |
518 | struct bt_ctf_stream_class *stream_class, const char *name) | |
519 | { | |
520 | size_t i; | |
69dc4535 JG |
521 | struct bt_ctf_event_class *event_class = NULL; |
522 | ||
523 | if (!stream_class || !name) { | |
524 | goto end; | |
525 | } | |
526 | ||
69dc4535 | 527 | for (i = 0; i < stream_class->event_classes->len; i++) { |
b8248cc0 | 528 | struct bt_ctf_event_class *cur_event_class = |
69dc4535 | 529 | g_ptr_array_index(stream_class->event_classes, i); |
b8248cc0 PP |
530 | const char *cur_event_class_name = |
531 | bt_ctf_event_class_get_name(cur_event_class); | |
69dc4535 | 532 | |
b8248cc0 PP |
533 | if (!strcmp(name, cur_event_class_name)) { |
534 | event_class = cur_event_class; | |
83509119 | 535 | bt_get(event_class); |
69dc4535 JG |
536 | goto end; |
537 | } | |
538 | } | |
539 | end: | |
540 | return event_class; | |
541 | } | |
542 | ||
0863f950 PP |
543 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id( |
544 | struct bt_ctf_stream_class *stream_class, uint32_t id) | |
545 | { | |
0b9ce69f | 546 | int64_t id_key = id; |
0863f950 PP |
547 | struct bt_ctf_event_class *event_class = NULL; |
548 | ||
549 | if (!stream_class) { | |
550 | goto end; | |
551 | } | |
552 | ||
0b9ce69f JG |
553 | event_class = g_hash_table_lookup(stream_class->event_classes_ht, |
554 | &id_key); | |
555 | bt_get(event_class); | |
0863f950 PP |
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 | ||
83509119 | 569 | bt_get(stream_class->packet_context_type); |
12c8a1a3 JG |
570 | ret = stream_class->packet_context_type; |
571 | end: | |
572 | return ret; | |
573 | } | |
574 | ||
575 | int bt_ctf_stream_class_set_packet_context_type( | |
576 | struct bt_ctf_stream_class *stream_class, | |
577 | struct bt_ctf_field_type *packet_context_type) | |
578 | { | |
579 | int ret = 0; | |
580 | ||
835b2d10 | 581 | if (!stream_class || stream_class->frozen) { |
12c8a1a3 JG |
582 | ret = -1; |
583 | goto end; | |
584 | } | |
585 | ||
835b2d10 JG |
586 | if (packet_context_type && |
587 | bt_ctf_field_type_get_type_id(packet_context_type) != | |
1487a16a | 588 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
835b2d10 | 589 | /* A packet context must be a structure. */ |
12c8a1a3 JG |
590 | ret = -1; |
591 | goto end; | |
592 | } | |
593 | ||
83509119 JG |
594 | bt_put(stream_class->packet_context_type); |
595 | bt_get(packet_context_type); | |
12c8a1a3 JG |
596 | stream_class->packet_context_type = packet_context_type; |
597 | end: | |
598 | return ret; | |
599 | } | |
600 | ||
662e778c JG |
601 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type( |
602 | struct bt_ctf_stream_class *stream_class) | |
603 | { | |
604 | struct bt_ctf_field_type *ret = NULL; | |
605 | ||
606 | if (!stream_class || !stream_class->event_header_type) { | |
607 | goto end; | |
608 | } | |
609 | ||
83509119 | 610 | bt_get(stream_class->event_header_type); |
662e778c JG |
611 | ret = stream_class->event_header_type; |
612 | end: | |
613 | return ret; | |
614 | } | |
615 | ||
616 | int bt_ctf_stream_class_set_event_header_type( | |
617 | struct bt_ctf_stream_class *stream_class, | |
618 | struct bt_ctf_field_type *event_header_type) | |
619 | { | |
620 | int ret = 0; | |
621 | ||
835b2d10 | 622 | if (!stream_class || stream_class->frozen) { |
662e778c JG |
623 | ret = -1; |
624 | goto end; | |
625 | } | |
626 | ||
835b2d10 JG |
627 | if (event_header_type && |
628 | bt_ctf_field_type_get_type_id(event_header_type) != | |
1487a16a | 629 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
835b2d10 | 630 | /* An event header must be a structure. */ |
662e778c JG |
631 | ret = -1; |
632 | goto end; | |
633 | } | |
634 | ||
83509119 | 635 | bt_put(stream_class->event_header_type); |
835b2d10 | 636 | stream_class->event_header_type = bt_get(event_header_type); |
662e778c JG |
637 | end: |
638 | return ret; | |
639 | } | |
640 | ||
af181248 JG |
641 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type( |
642 | struct bt_ctf_stream_class *stream_class) | |
643 | { | |
644 | struct bt_ctf_field_type *ret = NULL; | |
645 | ||
646 | if (!stream_class || !stream_class->event_context_type) { | |
647 | goto end; | |
648 | } | |
649 | ||
83509119 | 650 | bt_get(stream_class->event_context_type); |
af181248 JG |
651 | ret = stream_class->event_context_type; |
652 | end: | |
653 | return ret; | |
654 | } | |
655 | ||
656 | int bt_ctf_stream_class_set_event_context_type( | |
657 | struct bt_ctf_stream_class *stream_class, | |
658 | struct bt_ctf_field_type *event_context_type) | |
659 | { | |
660 | int ret = 0; | |
661 | ||
835b2d10 | 662 | if (!stream_class || stream_class->frozen) { |
af181248 JG |
663 | ret = -1; |
664 | goto end; | |
665 | } | |
666 | ||
835b2d10 JG |
667 | if (event_context_type && |
668 | bt_ctf_field_type_get_type_id(event_context_type) != | |
1487a16a | 669 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
835b2d10 | 670 | /* A packet context must be a structure. */ |
af181248 JG |
671 | ret = -1; |
672 | goto end; | |
673 | } | |
674 | ||
83509119 | 675 | bt_put(stream_class->event_context_type); |
835b2d10 | 676 | stream_class->event_context_type = bt_get(event_context_type); |
af181248 JG |
677 | end: |
678 | return ret; | |
679 | } | |
680 | ||
11b0cdc8 JG |
681 | void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class) |
682 | { | |
83509119 | 683 | bt_get(stream_class); |
11b0cdc8 JG |
684 | } |
685 | ||
686 | void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class) | |
687 | { | |
83509119 | 688 | bt_put(stream_class); |
11b0cdc8 JG |
689 | } |
690 | ||
8bf65fbd JG |
691 | static |
692 | int get_event_class_count(void *element) | |
693 | { | |
694 | return bt_ctf_stream_class_get_event_class_count( | |
695 | (struct bt_ctf_stream_class *) element); | |
696 | } | |
697 | ||
698 | static | |
699 | void *get_event_class(void *element, int i) | |
700 | { | |
701 | return bt_ctf_stream_class_get_event_class( | |
702 | (struct bt_ctf_stream_class *) element, i); | |
703 | } | |
704 | ||
705 | static | |
d9a13d86 | 706 | int visit_event_class(void *object, bt_ctf_visitor visitor,void *data) |
8bf65fbd | 707 | { |
d9a13d86 PP |
708 | struct bt_ctf_object obj = |
709 | { .object = object, | |
710 | .type = BT_CTF_OBJECT_TYPE_EVENT_CLASS }; | |
8bf65fbd | 711 | |
d9a13d86 | 712 | return visitor(&obj, data); |
8bf65fbd JG |
713 | } |
714 | ||
715 | int bt_ctf_stream_class_visit(struct bt_ctf_stream_class *stream_class, | |
d9a13d86 | 716 | bt_ctf_visitor visitor, void *data) |
8bf65fbd JG |
717 | { |
718 | int ret; | |
d9a13d86 PP |
719 | struct bt_ctf_object obj = |
720 | { .object = stream_class, | |
721 | .type = BT_CTF_OBJECT_TYPE_STREAM_CLASS }; | |
8bf65fbd JG |
722 | |
723 | if (!stream_class || !visitor) { | |
724 | ret = -1; | |
725 | goto end; | |
726 | } | |
727 | ||
d9a13d86 | 728 | ret = visitor_helper(&obj, get_event_class_count, |
8bf65fbd JG |
729 | get_event_class, |
730 | visit_event_class, visitor, data); | |
731 | end: | |
732 | return ret; | |
733 | } | |
734 | ||
11b0cdc8 JG |
735 | BT_HIDDEN |
736 | void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class) | |
737 | { | |
738 | if (!stream_class) { | |
739 | return; | |
740 | } | |
741 | ||
742 | stream_class->frozen = 1; | |
662e778c | 743 | bt_ctf_field_type_freeze(stream_class->event_header_type); |
12c8a1a3 | 744 | bt_ctf_field_type_freeze(stream_class->packet_context_type); |
af181248 | 745 | bt_ctf_field_type_freeze(stream_class->event_context_type); |
ac0c6bdd PP |
746 | |
747 | if (stream_class->clock) { | |
748 | bt_ctf_clock_class_freeze(stream_class->clock->clock_class); | |
749 | } | |
11b0cdc8 JG |
750 | } |
751 | ||
11b0cdc8 JG |
752 | BT_HIDDEN |
753 | int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class, | |
754 | struct metadata_context *context) | |
755 | { | |
2f100782 | 756 | int64_t ret = 0; |
11b0cdc8 JG |
757 | size_t i; |
758 | ||
759 | g_string_assign(context->field_name, ""); | |
760 | context->current_indentation_level = 1; | |
761 | if (!stream_class->id_set) { | |
762 | ret = -1; | |
763 | goto end; | |
764 | } | |
765 | ||
766 | g_string_append_printf(context->string, | |
767 | "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ", | |
768 | stream_class->id); | |
769 | ret = bt_ctf_field_type_serialize(stream_class->event_header_type, | |
770 | context); | |
771 | if (ret) { | |
772 | goto end; | |
773 | } | |
774 | ||
98edd02c JG |
775 | if (stream_class->packet_context_type) { |
776 | g_string_append(context->string, ";\n\n\tpacket.context := "); | |
777 | ret = bt_ctf_field_type_serialize(stream_class->packet_context_type, | |
778 | context); | |
779 | if (ret) { | |
780 | goto end; | |
781 | } | |
11b0cdc8 JG |
782 | } |
783 | ||
784 | if (stream_class->event_context_type) { | |
785 | g_string_append(context->string, ";\n\n\tevent.context := "); | |
786 | ret = bt_ctf_field_type_serialize( | |
787 | stream_class->event_context_type, context); | |
788 | if (ret) { | |
789 | goto end; | |
790 | } | |
791 | } | |
792 | ||
793 | g_string_append(context->string, ";\n};\n\n"); | |
11b0cdc8 JG |
794 | for (i = 0; i < stream_class->event_classes->len; i++) { |
795 | struct bt_ctf_event_class *event_class = | |
796 | stream_class->event_classes->pdata[i]; | |
797 | ||
11b0cdc8 JG |
798 | ret = bt_ctf_event_class_serialize(event_class, context); |
799 | if (ret) { | |
800 | goto end; | |
801 | } | |
802 | } | |
803 | end: | |
804 | context->current_indentation_level = 0; | |
805 | return ret; | |
806 | } | |
807 | ||
808 | static | |
83509119 | 809 | void bt_ctf_stream_class_destroy(struct bt_object *obj) |
11b0cdc8 JG |
810 | { |
811 | struct bt_ctf_stream_class *stream_class; | |
812 | ||
83509119 JG |
813 | stream_class = container_of(obj, struct bt_ctf_stream_class, base); |
814 | bt_put(stream_class->clock); | |
11b0cdc8 | 815 | |
0b9ce69f JG |
816 | if (stream_class->event_classes_ht) { |
817 | g_hash_table_destroy(stream_class->event_classes_ht); | |
818 | } | |
11b0cdc8 JG |
819 | if (stream_class->event_classes) { |
820 | g_ptr_array_free(stream_class->event_classes, TRUE); | |
821 | } | |
822 | ||
823 | if (stream_class->name) { | |
824 | g_string_free(stream_class->name, TRUE); | |
825 | } | |
826 | ||
83509119 JG |
827 | bt_put(stream_class->event_header_type); |
828 | bt_put(stream_class->packet_context_type); | |
829 | bt_put(stream_class->event_context_type); | |
11b0cdc8 JG |
830 | g_free(stream_class); |
831 | } | |
832 | ||
833 | static | |
662e778c | 834 | int init_event_header(struct bt_ctf_stream_class *stream_class) |
11b0cdc8 JG |
835 | { |
836 | int ret = 0; | |
837 | struct bt_ctf_field_type *event_header_type = | |
838 | bt_ctf_field_type_structure_create(); | |
839 | struct bt_ctf_field_type *_uint32_t = | |
840 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); | |
841 | struct bt_ctf_field_type *_uint64_t = | |
842 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); | |
843 | ||
844 | if (!event_header_type) { | |
845 | ret = -1; | |
846 | goto end; | |
847 | } | |
848 | ||
11b0cdc8 JG |
849 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
850 | _uint32_t, "id"); | |
851 | if (ret) { | |
852 | goto end; | |
853 | } | |
854 | ||
855 | ret = bt_ctf_field_type_structure_add_field(event_header_type, | |
856 | _uint64_t, "timestamp"); | |
857 | if (ret) { | |
858 | goto end; | |
859 | } | |
860 | ||
662e778c | 861 | if (stream_class->event_header_type) { |
83509119 | 862 | bt_put(stream_class->event_header_type); |
de876b7f | 863 | } |
662e778c | 864 | stream_class->event_header_type = event_header_type; |
11b0cdc8 JG |
865 | end: |
866 | if (ret) { | |
83509119 | 867 | bt_put(event_header_type); |
11b0cdc8 JG |
868 | } |
869 | ||
83509119 JG |
870 | bt_put(_uint32_t); |
871 | bt_put(_uint64_t); | |
11b0cdc8 JG |
872 | return ret; |
873 | } | |
874 | ||
875 | static | |
662e778c | 876 | int init_packet_context(struct bt_ctf_stream_class *stream_class) |
11b0cdc8 JG |
877 | { |
878 | int ret = 0; | |
879 | struct bt_ctf_field_type *packet_context_type = | |
880 | bt_ctf_field_type_structure_create(); | |
881 | struct bt_ctf_field_type *_uint64_t = | |
882 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); | |
883 | ||
884 | if (!packet_context_type) { | |
885 | ret = -1; | |
886 | goto end; | |
887 | } | |
888 | ||
889 | /* | |
890 | * We create a stream packet context as proposed in the CTF | |
891 | * specification. | |
892 | */ | |
11b0cdc8 JG |
893 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
894 | _uint64_t, "timestamp_begin"); | |
895 | if (ret) { | |
896 | goto end; | |
897 | } | |
898 | ||
899 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
900 | _uint64_t, "timestamp_end"); | |
901 | if (ret) { | |
902 | goto end; | |
903 | } | |
904 | ||
905 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
906 | _uint64_t, "content_size"); | |
907 | if (ret) { | |
908 | goto end; | |
909 | } | |
910 | ||
911 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
912 | _uint64_t, "packet_size"); | |
913 | if (ret) { | |
914 | goto end; | |
915 | } | |
916 | ||
917 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
918 | _uint64_t, "events_discarded"); | |
919 | if (ret) { | |
920 | goto end; | |
921 | } | |
922 | ||
83509119 | 923 | bt_put(stream_class->packet_context_type); |
11b0cdc8 | 924 | stream_class->packet_context_type = packet_context_type; |
11b0cdc8 JG |
925 | end: |
926 | if (ret) { | |
83509119 | 927 | bt_put(packet_context_type); |
11b0cdc8 JG |
928 | goto end; |
929 | } | |
930 | ||
83509119 | 931 | bt_put(_uint64_t); |
11b0cdc8 JG |
932 | return ret; |
933 | } |