4 * Babeltrace CTF IR - Event
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/ctf-writer/event.h>
30 #include <babeltrace/ctf-writer/event-types.h>
31 #include <babeltrace/ctf-writer/event-fields.h>
32 #include <babeltrace/ctf-ir/event-fields-internal.h>
33 #include <babeltrace/ctf-ir/event-types-internal.h>
34 #include <babeltrace/ctf-ir/event-internal.h>
35 #include <babeltrace/ctf-ir/stream-class.h>
36 #include <babeltrace/ctf-ir/stream-class-internal.h>
37 #include <babeltrace/ctf-ir/trace-internal.h>
38 #include <babeltrace/ctf-ir/utils.h>
39 #include <babeltrace/compiler.h>
42 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
);
44 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
);
46 struct bt_ctf_event_class
*bt_ctf_event_class_create(const char *name
)
48 struct bt_ctf_event_class
*event_class
= NULL
;
50 if (bt_ctf_validate_identifier(name
)) {
54 event_class
= g_new0(struct bt_ctf_event_class
, 1);
59 bt_ctf_ref_init(&event_class
->ref_count
);
60 event_class
->name
= g_quark_from_string(name
);
65 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class
*event_class
)
67 const char *name
= NULL
;
73 name
= g_quark_to_string(event_class
->name
);
78 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
82 if (!event_class
|| !event_class
->id_set
) {
87 ret
= (int64_t) event_class
->id
;
92 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
102 if (event_class
->stream_class
) {
104 * We don't allow changing the id if the event class has already
105 * been added to a stream class.
111 event_class
->id
= id
;
112 event_class
->id_set
= 1;
117 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
118 struct bt_ctf_event_class
*event_class
)
120 struct bt_ctf_stream_class
*stream_class
= NULL
;
126 stream_class
= event_class
->stream_class
;
127 bt_ctf_stream_class_get(stream_class
);
132 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
133 struct bt_ctf_field_type
*type
,
138 if (!event_class
|| !type
|| bt_ctf_validate_identifier(name
) ||
139 event_class
->frozen
) {
144 if (!event_class
->fields
) {
145 event_class
->fields
= bt_ctf_field_type_structure_create();
146 if (!event_class
->fields
) {
152 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
158 int bt_ctf_event_class_get_field_count(
159 struct bt_ctf_event_class
*event_class
)
168 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
173 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
174 const char **field_name
, struct bt_ctf_field_type
**field_type
,
179 if (!event_class
|| index
< 0) {
184 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
185 field_name
, field_type
, index
);
190 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
191 struct bt_ctf_event_class
*event_class
, const char *name
)
194 struct bt_ctf_field_type
*field_type
= NULL
;
196 if (!event_class
|| !name
) {
200 name_quark
= g_quark_try_string(name
);
206 * No need to increment field_type's reference count since getting it
207 * from the structure already does.
209 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
210 event_class
->fields
, name
);
215 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
216 struct bt_ctf_event_class
*event_class
)
218 struct bt_ctf_field_type
*context_type
= NULL
;
220 if (!event_class
|| !event_class
->context
) {
224 bt_ctf_field_type_get(event_class
->context
);
225 context_type
= event_class
->context
;
230 int bt_ctf_event_class_set_context_type(
231 struct bt_ctf_event_class
*event_class
,
232 struct bt_ctf_field_type
*context
)
236 if (!event_class
|| !context
|| event_class
->frozen
) {
241 if (bt_ctf_field_type_get_type_id(context
) != CTF_TYPE_STRUCT
) {
246 bt_ctf_field_type_get(context
);
247 bt_ctf_field_type_put(event_class
->context
);
248 event_class
->context
= context
;
254 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
260 bt_ctf_ref_get(&event_class
->ref_count
);
263 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
269 bt_ctf_ref_put(&event_class
->ref_count
, bt_ctf_event_class_destroy
);
272 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
274 struct bt_ctf_event
*event
= NULL
;
280 event
= g_new0(struct bt_ctf_event
, 1);
285 bt_ctf_ref_init(&event
->ref_count
);
286 bt_ctf_event_class_get(event_class
);
287 bt_ctf_event_class_freeze(event_class
);
288 event
->event_class
= event_class
;
289 if (event_class
->context
) {
290 event
->context_payload
= bt_ctf_field_create(
291 event_class
->context
);
293 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
298 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
300 struct bt_ctf_event_class
*event_class
= NULL
;
306 event_class
= event
->event_class
;
307 bt_ctf_event_class_get(event_class
);
312 struct bt_ctf_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
314 struct bt_ctf_clock
*clock
= NULL
;
315 struct bt_ctf_event_class
*event_class
;
316 struct bt_ctf_stream_class
*stream_class
;
322 event_class
= bt_ctf_event_get_class(event
);
327 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
329 goto error_put_event_class
;
332 clock
= bt_ctf_stream_class_get_clock(stream_class
);
334 goto error_put_stream_class
;
337 error_put_stream_class
:
338 bt_ctf_stream_class_put(stream_class
);
339 error_put_event_class
:
340 bt_ctf_event_class_put(event_class
);
345 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
347 struct bt_ctf_field
*value
)
351 if (!event
|| !value
|| bt_ctf_validate_identifier(name
)) {
356 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
363 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
366 struct bt_ctf_field
*field
= NULL
;
368 if (!event
|| !name
) {
372 field
= bt_ctf_field_structure_get_field(event
->fields_payload
, name
);
377 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
378 struct bt_ctf_event
*event
, int index
)
380 struct bt_ctf_field
*field
= NULL
;
382 if (!event
|| index
< 0) {
386 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
392 struct bt_ctf_field
*bt_ctf_event_get_event_context(
393 struct bt_ctf_event
*event
)
395 struct bt_ctf_field
*context
= NULL
;
397 if (!event
|| !event
->context_payload
) {
401 context
= event
->context_payload
;
402 bt_ctf_field_get(context
);
407 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
408 struct bt_ctf_field
*context
)
411 struct bt_ctf_field_type
*field_type
= NULL
;
413 if (!event
|| !context
) {
418 field_type
= bt_ctf_field_get_type(context
);
419 if (field_type
!= event
->event_class
->context
) {
424 bt_ctf_field_get(context
);
425 bt_ctf_field_put(event
->context_payload
);
426 event
->context_payload
= context
;
429 bt_ctf_field_type_put(field_type
);
434 void bt_ctf_event_get(struct bt_ctf_event
*event
)
440 bt_ctf_ref_get(&event
->ref_count
);
443 void bt_ctf_event_put(struct bt_ctf_event
*event
)
449 bt_ctf_ref_put(&event
->ref_count
, bt_ctf_event_destroy
);
453 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
)
455 struct bt_ctf_event_class
*event_class
;
462 * Don't call put() on the stream class. See comment in
463 * bt_ctf_event_class_set_stream_class for explanation.
465 event_class
= container_of(ref
, struct bt_ctf_event_class
, ref_count
);
466 if (event_class
->context
) {
467 bt_ctf_field_type_put(event_class
->context
);
469 if (event_class
->fields
) {
470 bt_ctf_field_type_put(event_class
->fields
);
476 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
)
478 struct bt_ctf_event
*event
;
484 event
= container_of(ref
, struct bt_ctf_event
,
486 if (event
->event_class
) {
487 bt_ctf_event_class_put(event
->event_class
);
489 if (event
->context_payload
) {
490 bt_ctf_field_put(event
->context_payload
);
492 if (event
->fields_payload
) {
493 bt_ctf_field_put(event
->fields_payload
);
499 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
502 event_class
->frozen
= 1;
503 bt_ctf_field_type_freeze(event_class
->context
);
504 bt_ctf_field_type_freeze(event_class
->fields
);
508 int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class
*event_class
,
509 struct bt_ctf_stream_class
*stream_class
)
518 /* Allow a NULL stream_class to unset the current stream_class */
519 if (stream_class
&& event_class
->stream_class
) {
524 event_class
->stream_class
= stream_class
;
526 * We don't get() the stream_class since doing so would introduce
527 * a circular ownership between event classes and stream classes.
529 * A stream class will always unset itself from its events before
530 * being destroyed. This ensures that a user won't get a pointer
531 * to a stale stream class instance from an event class.
538 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
539 struct metadata_context
*context
)
546 stream_id
= bt_ctf_stream_class_get_id(event_class
->stream_class
);
552 context
->current_indentation_level
= 1;
553 g_string_assign(context
->field_name
, "");
554 g_string_append_printf(context
->string
, "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRId64
";\n",
555 g_quark_to_string(event_class
->name
),
559 if (event_class
->context
) {
560 g_string_append(context
->string
, "\tcontext := ");
561 ret
= bt_ctf_field_type_serialize(event_class
->context
,
566 g_string_append(context
->string
, ";\n");
569 if (event_class
->fields
) {
570 g_string_append(context
->string
, "\tfields := ");
571 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
575 g_string_append(context
->string
, ";\n");
578 g_string_append(context
->string
, "};\n\n");
580 context
->current_indentation_level
= 0;
584 void bt_ctf_event_class_set_native_byte_order(
585 struct bt_ctf_event_class
*event_class
,
592 bt_ctf_field_type_set_native_byte_order(event_class
->context
,
594 bt_ctf_field_type_set_native_byte_order(event_class
->fields
,
599 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
601 /* Make sure each field's payload has been set */
605 ret
= bt_ctf_field_validate(event
->fields_payload
);
610 if (event
->event_class
->context
) {
611 ret
= bt_ctf_field_validate(event
->context_payload
);
618 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
619 struct ctf_stream_pos
*pos
)
625 if (event
->context_payload
) {
626 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
632 if (event
->fields_payload
) {
633 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
643 int bt_ctf_event_set_timestamp(struct bt_ctf_event
*event
,
649 if (event
->timestamp
) {
654 event
->timestamp
= timestamp
;
660 uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event
*event
)
663 return event
->timestamp
;