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