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/trace-internal.h>
37 #include <babeltrace/compiler.h>
40 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
);
42 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
);
44 struct bt_ctf_event_class
*bt_ctf_event_class_create(const char *name
)
46 struct bt_ctf_event_class
*event_class
= NULL
;
48 if (validate_identifier(name
)) {
52 event_class
= g_new0(struct bt_ctf_event_class
, 1);
57 bt_ctf_ref_init(&event_class
->ref_count
);
58 event_class
->name
= g_quark_from_string(name
);
63 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class
*event_class
)
65 const char *name
= NULL
;
71 name
= g_quark_to_string(event_class
->name
);
76 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
80 if (!event_class
|| !event_class
->id_set
) {
85 ret
= (int64_t) event_class
->id
;
90 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
100 if (event_class
->stream_class
) {
102 * We don't allow changing the id if the event class has already
103 * been added to a stream class.
109 event_class
->id
= id
;
110 event_class
->id_set
= 1;
115 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
116 struct bt_ctf_event_class
*event_class
)
118 struct bt_ctf_stream_class
*stream_class
= NULL
;
124 stream_class
= event_class
->stream_class
;
125 bt_ctf_stream_class_get(stream_class
);
130 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
131 struct bt_ctf_field_type
*type
,
136 if (!event_class
|| !type
|| validate_identifier(name
) ||
137 event_class
->frozen
) {
142 if (!event_class
->fields
) {
143 event_class
->fields
= bt_ctf_field_type_structure_create();
144 if (!event_class
->fields
) {
150 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
156 int bt_ctf_event_class_get_field_count(
157 struct bt_ctf_event_class
*event_class
)
166 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
171 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
172 const char **field_name
, struct bt_ctf_field_type
**field_type
,
177 if (!event_class
|| index
< 0) {
182 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
183 field_name
, field_type
, index
);
188 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
189 struct bt_ctf_event_class
*event_class
, const char *name
)
192 struct bt_ctf_field_type
*field_type
= NULL
;
194 if (!event_class
|| !name
) {
198 name_quark
= g_quark_try_string(name
);
204 * No need to increment field_type's reference count since getting it
205 * from the structure already does.
207 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
208 event_class
->fields
, name
);
213 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
214 struct bt_ctf_event_class
*event_class
)
216 struct bt_ctf_field_type
*context_type
= NULL
;
218 if (!event_class
|| !event_class
->context
) {
222 bt_ctf_field_type_get(event_class
->context
);
223 context_type
= event_class
->context
;
228 int bt_ctf_event_class_set_context_type(
229 struct bt_ctf_event_class
*event_class
,
230 struct bt_ctf_field_type
*context
)
234 if (!event_class
|| !context
|| event_class
->frozen
) {
239 if (bt_ctf_field_type_get_type_id(context
) != CTF_TYPE_STRUCT
) {
244 bt_ctf_field_type_get(context
);
245 bt_ctf_field_type_put(event_class
->context
);
246 event_class
->context
= context
;
252 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
258 bt_ctf_ref_get(&event_class
->ref_count
);
261 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
267 bt_ctf_ref_put(&event_class
->ref_count
, bt_ctf_event_class_destroy
);
270 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
272 struct bt_ctf_event
*event
= NULL
;
278 event
= g_new0(struct bt_ctf_event
, 1);
283 bt_ctf_ref_init(&event
->ref_count
);
284 bt_ctf_event_class_get(event_class
);
285 bt_ctf_event_class_freeze(event_class
);
286 event
->event_class
= event_class
;
287 if (event_class
->context
) {
288 event
->context_payload
= bt_ctf_field_create(
289 event_class
->context
);
291 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
296 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
298 struct bt_ctf_event_class
*event_class
= NULL
;
304 event_class
= event
->event_class
;
305 bt_ctf_event_class_get(event_class
);
310 struct bt_ctf_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
312 struct bt_ctf_clock
*clock
= NULL
;
313 struct bt_ctf_event_class
*event_class
;
314 struct bt_ctf_stream_class
*stream_class
;
320 event_class
= bt_ctf_event_get_class(event
);
325 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
327 goto error_put_event_class
;
330 clock
= bt_ctf_stream_class_get_clock(stream_class
);
332 goto error_put_stream_class
;
335 error_put_stream_class
:
336 bt_ctf_stream_class_put(stream_class
);
337 error_put_event_class
:
338 bt_ctf_event_class_put(event_class
);
343 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
345 struct bt_ctf_field
*value
)
349 if (!event
|| !value
|| validate_identifier(name
)) {
354 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
361 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
364 struct bt_ctf_field
*field
= NULL
;
366 if (!event
|| !name
) {
370 field
= bt_ctf_field_structure_get_field(event
->fields_payload
, name
);
375 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
376 struct bt_ctf_event
*event
, int index
)
378 struct bt_ctf_field
*field
= NULL
;
380 if (!event
|| index
< 0) {
384 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
390 struct bt_ctf_field
*bt_ctf_event_get_event_context(
391 struct bt_ctf_event
*event
)
393 struct bt_ctf_field
*context
= NULL
;
395 if (!event
|| !event
->context_payload
) {
399 context
= event
->context_payload
;
400 bt_ctf_field_get(context
);
405 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
406 struct bt_ctf_field
*context
)
409 struct bt_ctf_field_type
*field_type
= NULL
;
411 if (!event
|| !context
) {
416 field_type
= bt_ctf_field_get_type(context
);
417 if (field_type
!= event
->event_class
->context
) {
422 bt_ctf_field_get(context
);
423 bt_ctf_field_put(event
->context_payload
);
424 event
->context_payload
= context
;
427 bt_ctf_field_type_put(field_type
);
432 void bt_ctf_event_get(struct bt_ctf_event
*event
)
438 bt_ctf_ref_get(&event
->ref_count
);
441 void bt_ctf_event_put(struct bt_ctf_event
*event
)
447 bt_ctf_ref_put(&event
->ref_count
, bt_ctf_event_destroy
);
451 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
)
453 struct bt_ctf_event_class
*event_class
;
460 * Don't call put() on the stream class. See comment in
461 * bt_ctf_event_class_set_stream_class for explanation.
463 event_class
= container_of(ref
, struct bt_ctf_event_class
, ref_count
);
464 if (event_class
->context
) {
465 bt_ctf_field_type_put(event_class
->context
);
467 if (event_class
->fields
) {
468 bt_ctf_field_type_put(event_class
->fields
);
474 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
)
476 struct bt_ctf_event
*event
;
482 event
= container_of(ref
, struct bt_ctf_event
,
484 if (event
->event_class
) {
485 bt_ctf_event_class_put(event
->event_class
);
487 if (event
->context_payload
) {
488 bt_ctf_field_put(event
->context_payload
);
490 if (event
->fields_payload
) {
491 bt_ctf_field_put(event
->fields_payload
);
497 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
500 event_class
->frozen
= 1;
501 bt_ctf_field_type_freeze(event_class
->context
);
502 bt_ctf_field_type_freeze(event_class
->fields
);
506 int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class
*event_class
,
507 struct bt_ctf_stream_class
*stream_class
)
516 /* Allow a NULL stream_class to unset the current stream_class */
517 if (stream_class
&& event_class
->stream_class
) {
522 event_class
->stream_class
= stream_class
;
524 * We don't get() the stream_class since doing so would introduce
525 * a circular ownership between event classes and stream classes.
527 * A stream class will always unset itself from its events before
528 * being destroyed. This ensures that a user won't get a pointer
529 * to a stale stream class instance from an event class.
536 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
537 struct metadata_context
*context
)
544 stream_id
= bt_ctf_stream_class_get_id(event_class
->stream_class
);
550 context
->current_indentation_level
= 1;
551 g_string_assign(context
->field_name
, "");
552 g_string_append_printf(context
->string
, "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRId64
";\n",
553 g_quark_to_string(event_class
->name
),
557 if (event_class
->context
) {
558 g_string_append(context
->string
, "\tcontext := ");
559 ret
= bt_ctf_field_type_serialize(event_class
->context
,
564 g_string_append(context
->string
, ";\n");
567 if (event_class
->fields
) {
568 g_string_append(context
->string
, "\tfields := ");
569 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
573 g_string_append(context
->string
, ";\n");
576 g_string_append(context
->string
, "};\n\n");
578 context
->current_indentation_level
= 0;
583 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
585 /* Make sure each field's payload has been set */
589 ret
= bt_ctf_field_validate(event
->fields_payload
);
594 if (event
->event_class
->context
) {
595 ret
= bt_ctf_field_validate(event
->context_payload
);
602 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
603 struct ctf_stream_pos
*pos
)
609 if (event
->context_payload
) {
610 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
616 if (event
->fields_payload
) {
617 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
627 int bt_ctf_event_set_timestamp(struct bt_ctf_event
*event
,
633 if (event
->timestamp
) {
638 event
->timestamp
= timestamp
;
644 uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event
*event
)
647 return event
->timestamp
;