Reuse previously allocated string when setting value
[babeltrace.git] / formats / ctf / ir / stream-class.c
CommitLineData
11b0cdc8 1/*
3f043b05 2 * stream-class.c
11b0cdc8
JG
3 *
4 * Babeltrace CTF Writer
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>
32#include <babeltrace/ctf-ir/event-internal.h>
33#include <babeltrace/ctf-ir/event-types-internal.h>
34#include <babeltrace/ctf-ir/event-fields-internal.h>
35#include <babeltrace/ctf-writer/stream.h>
36#include <babeltrace/ctf-ir/stream-class-internal.h>
37#include <babeltrace/ctf-writer/functor-internal.h>
38#include <babeltrace/compiler.h>
39#include <babeltrace/align.h>
40
41static
42void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref);
43static
44int init_event_header(struct bt_ctf_stream_class *stream_class,
45 enum bt_ctf_byte_order byte_order);
46static
47int init_packet_context(struct bt_ctf_stream_class *stream_class,
48 enum bt_ctf_byte_order byte_order);
49
50struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
51{
52 struct bt_ctf_stream_class *stream_class = NULL;
53
54 if (!name || !strlen(name)) {
55 goto error;
56 }
57
58 stream_class = g_new0(struct bt_ctf_stream_class, 1);
59 if (!stream_class) {
60 goto error;
61 }
62
63 stream_class->name = g_string_new(name);
64 stream_class->event_classes = g_ptr_array_new_with_free_func(
65 (GDestroyNotify)bt_ctf_event_class_put);
66 if (!stream_class->event_classes) {
67 goto error_destroy;
68 }
69
70 bt_ctf_ref_init(&stream_class->ref_count);
71 return stream_class;
72
73error_destroy:
74 bt_ctf_stream_class_destroy(&stream_class->ref_count);
75 stream_class = NULL;
76error:
77 return stream_class;
78}
79
69dc4535
JG
80const char *bt_ctf_stream_class_get_name(
81 struct bt_ctf_stream_class *stream_class)
82{
83 const char *name = NULL;
84
85 if (!stream_class) {
86 goto end;
87 }
88
89 name = stream_class->name->str;
90end:
91 return name;
92}
93
2f100782
JG
94struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
95 struct bt_ctf_stream_class *stream_class)
96{
97 struct bt_ctf_clock *clock = NULL;
98
99 if (!stream_class || !stream_class->clock) {
100 goto end;
101 }
102
103 clock = stream_class->clock;
104 bt_ctf_clock_get(clock);
105end:
106 return clock;
107}
108
11b0cdc8
JG
109int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
110 struct bt_ctf_clock *clock)
111{
112 int ret = 0;
113
114 if (!stream_class || !clock || stream_class->frozen) {
115 ret = -1;
116 goto end;
117 }
118
119 if (stream_class->clock) {
120 bt_ctf_clock_put(stream_class->clock);
121 }
122
123 stream_class->clock = clock;
124 bt_ctf_clock_get(clock);
125end:
126 return ret;
127}
128
2f100782
JG
129int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class)
130{
131 int64_t ret;
132
133 if (!stream_class || !stream_class->id_set) {
134 ret = -1;
135 goto end;
136 }
137
138 ret = (int64_t) stream_class->id;
139end:
140 return ret;
141}
142
143int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
144 uint32_t id)
145{
146 int ret = 0;
147
148 if (!stream_class) {
149 ret = -1;
150 goto end;
151 }
152
153 stream_class->id = id;
154 stream_class->id_set = 1;
155end:
156 return ret;
157}
158
11b0cdc8
JG
159int bt_ctf_stream_class_add_event_class(
160 struct bt_ctf_stream_class *stream_class,
161 struct bt_ctf_event_class *event_class)
162{
163 int ret = 0;
2f100782 164 int64_t event_id;
11b0cdc8
JG
165
166 if (!stream_class || !event_class) {
167 ret = -1;
168 goto end;
169 }
170
171 /* Check for duplicate event classes */
172 struct search_query query = { .value = event_class, .found = 0 };
173 g_ptr_array_foreach(stream_class->event_classes, value_exists, &query);
174 if (query.found) {
175 ret = -1;
176 goto end;
177 }
178
2f100782
JG
179 /* Only set an event id if none was explicitly set before */
180 event_id = bt_ctf_event_class_get_id(event_class);
181 if (event_id < 0) {
182 if (bt_ctf_event_class_set_id(event_class,
183 stream_class->next_event_id++)) {
184 ret = -1;
185 goto end;
186 }
187 }
188
189 ret = bt_ctf_event_class_set_stream_class(event_class, stream_class);
190 if (ret) {
11b0cdc8
JG
191 goto end;
192 }
193
194 bt_ctf_event_class_get(event_class);
195 g_ptr_array_add(stream_class->event_classes, event_class);
196end:
197 return ret;
198}
199
69dc4535
JG
200int64_t bt_ctf_stream_class_get_event_class_count(
201 struct bt_ctf_stream_class *stream_class)
202{
203 int64_t ret;
204
205 if (!stream_class) {
206 ret = -1;
207 goto end;
208 }
209
210 ret = (int64_t) stream_class->event_classes->len;
211end:
212 return ret;
213}
214
215struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
216 struct bt_ctf_stream_class *stream_class, size_t index)
217{
218 struct bt_ctf_event_class *event_class = NULL;
219
220 if (!stream_class || index >= stream_class->event_classes->len) {
221 goto end;
222 }
223
224 event_class = g_ptr_array_index(stream_class->event_classes, index);
225 bt_ctf_event_class_get(event_class);
226end:
227 return event_class;
228}
229
230struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
231 struct bt_ctf_stream_class *stream_class, const char *name)
232{
233 size_t i;
234 GQuark name_quark;
235 struct bt_ctf_event_class *event_class = NULL;
236
237 if (!stream_class || !name) {
238 goto end;
239 }
240
241 name_quark = g_quark_try_string(name);
242 if (!name_quark) {
243 goto end;
244 }
245
246 for (i = 0; i < stream_class->event_classes->len; i++) {
247 struct bt_ctf_event_class *current_event_class =
248 g_ptr_array_index(stream_class->event_classes, i);
249
250 if (name_quark == current_event_class->name) {
251 event_class = current_event_class;
252 bt_ctf_event_class_get(event_class);
253 goto end;
254 }
255 }
256end:
257 return event_class;
258}
259
11b0cdc8
JG
260void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
261{
262 if (!stream_class) {
263 return;
264 }
265
266 bt_ctf_ref_get(&stream_class->ref_count);
267}
268
269void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
270{
271 if (!stream_class) {
272 return;
273 }
274
275 bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy);
276}
277
278BT_HIDDEN
279void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
280{
281 if (!stream_class) {
282 return;
283 }
284
285 stream_class->frozen = 1;
286 bt_ctf_clock_freeze(stream_class->clock);
287 g_ptr_array_foreach(stream_class->event_classes,
288 (GFunc)bt_ctf_event_class_freeze, NULL);
289}
290
11b0cdc8
JG
291BT_HIDDEN
292int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
293 enum bt_ctf_byte_order byte_order)
294{
295 int ret = 0;
296
297 ret = init_packet_context(stream_class, byte_order);
298 if (ret) {
299 goto end;
300 }
301
302 ret = init_event_header(stream_class, byte_order);
303 if (ret) {
304 goto end;
305 }
306end:
307 return ret;
308}
309
310BT_HIDDEN
311int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
312 struct metadata_context *context)
313{
2f100782 314 int64_t ret = 0;
11b0cdc8
JG
315 size_t i;
316
317 g_string_assign(context->field_name, "");
318 context->current_indentation_level = 1;
319 if (!stream_class->id_set) {
320 ret = -1;
321 goto end;
322 }
323
324 g_string_append_printf(context->string,
325 "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ",
326 stream_class->id);
327 ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
328 context);
329 if (ret) {
330 goto end;
331 }
332
333 g_string_append(context->string, ";\n\n\tpacket.context := ");
334 ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
335 context);
336 if (ret) {
337 goto end;
338 }
339
340 if (stream_class->event_context_type) {
341 g_string_append(context->string, ";\n\n\tevent.context := ");
342 ret = bt_ctf_field_type_serialize(
343 stream_class->event_context_type, context);
344 if (ret) {
345 goto end;
346 }
347 }
348
349 g_string_append(context->string, ";\n};\n\n");
11b0cdc8
JG
350 for (i = 0; i < stream_class->event_classes->len; i++) {
351 struct bt_ctf_event_class *event_class =
352 stream_class->event_classes->pdata[i];
353
11b0cdc8
JG
354 ret = bt_ctf_event_class_serialize(event_class, context);
355 if (ret) {
356 goto end;
357 }
358 }
359end:
360 context->current_indentation_level = 0;
361 return ret;
362}
363
364static
365void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
366{
367 struct bt_ctf_stream_class *stream_class;
368
369 if (!ref) {
370 return;
371 }
372
373 stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count);
374 bt_ctf_clock_put(stream_class->clock);
375
376 if (stream_class->event_classes) {
2f100782
JG
377 size_t i;
378
379 /* Unregister this stream class from the event classes */
380 for (i = 0; i < stream_class->event_classes->len; i++) {
381 struct bt_ctf_event_class *event_class =
382 g_ptr_array_index(stream_class->event_classes,
383 i);
384
385 bt_ctf_event_class_set_stream_class(event_class, NULL);
386 }
387
11b0cdc8
JG
388 g_ptr_array_free(stream_class->event_classes, TRUE);
389 }
390
391 if (stream_class->name) {
392 g_string_free(stream_class->name, TRUE);
393 }
394
395 bt_ctf_field_type_put(stream_class->event_header_type);
396 bt_ctf_field_put(stream_class->event_header);
397 bt_ctf_field_type_put(stream_class->packet_context_type);
398 bt_ctf_field_put(stream_class->packet_context);
399 bt_ctf_field_type_put(stream_class->event_context_type);
400 bt_ctf_field_put(stream_class->event_context);
401 g_free(stream_class);
402}
403
404static
405int init_event_header(struct bt_ctf_stream_class *stream_class,
406 enum bt_ctf_byte_order byte_order)
407{
408 int ret = 0;
409 struct bt_ctf_field_type *event_header_type =
410 bt_ctf_field_type_structure_create();
411 struct bt_ctf_field_type *_uint32_t =
412 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
413 struct bt_ctf_field_type *_uint64_t =
414 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
415
416 if (!event_header_type) {
417 ret = -1;
418 goto end;
419 }
420
421 ret = bt_ctf_field_type_set_byte_order(_uint32_t, byte_order);
422 if (ret) {
423 goto end;
424 }
425
426 ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
427 if (ret) {
428 goto end;
429 }
430
431 ret = bt_ctf_field_type_structure_add_field(event_header_type,
432 _uint32_t, "id");
433 if (ret) {
434 goto end;
435 }
436
437 ret = bt_ctf_field_type_structure_add_field(event_header_type,
438 _uint64_t, "timestamp");
439 if (ret) {
440 goto end;
441 }
442
443 stream_class->event_header_type = event_header_type;
444 stream_class->event_header = bt_ctf_field_create(
445 stream_class->event_header_type);
446 if (!stream_class->event_header) {
447 ret = -1;
448 }
449end:
450 if (ret) {
451 bt_ctf_field_type_put(event_header_type);
452 }
453
454 bt_ctf_field_type_put(_uint32_t);
455 bt_ctf_field_type_put(_uint64_t);
456 return ret;
457}
458
459static
460int init_packet_context(struct bt_ctf_stream_class *stream_class,
461 enum bt_ctf_byte_order byte_order)
462{
463 int ret = 0;
464 struct bt_ctf_field_type *packet_context_type =
465 bt_ctf_field_type_structure_create();
466 struct bt_ctf_field_type *_uint64_t =
467 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
468
469 if (!packet_context_type) {
470 ret = -1;
471 goto end;
472 }
473
474 /*
475 * We create a stream packet context as proposed in the CTF
476 * specification.
477 */
478 ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
479 if (ret) {
480 goto end;
481 }
482
483 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
484 _uint64_t, "timestamp_begin");
485 if (ret) {
486 goto end;
487 }
488
489 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
490 _uint64_t, "timestamp_end");
491 if (ret) {
492 goto end;
493 }
494
495 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
496 _uint64_t, "content_size");
497 if (ret) {
498 goto end;
499 }
500
501 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
502 _uint64_t, "packet_size");
503 if (ret) {
504 goto end;
505 }
506
507 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
508 _uint64_t, "events_discarded");
509 if (ret) {
510 goto end;
511 }
512
513 stream_class->packet_context_type = packet_context_type;
514 stream_class->packet_context = bt_ctf_field_create(packet_context_type);
515 if (!stream_class->packet_context) {
516 ret = -1;
517 }
518end:
519 if (ret) {
520 bt_ctf_field_type_put(packet_context_type);
521 goto end;
522 }
523
524 bt_ctf_field_type_put(_uint64_t);
525 return ret;
526}
This page took 0.041863 seconds and 4 git commands to generate.