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/ctf-ir/utils.h>
38 #include <babeltrace/compiler.h>
41 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
);
43 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
);
45 struct bt_ctf_event_class
*bt_ctf_event_class_create(const char *name
)
47 struct bt_ctf_event_class
*event_class
= NULL
;
49 if (bt_ctf_validate_identifier(name
)) {
53 event_class
= g_new0(struct bt_ctf_event_class
, 1);
58 bt_ctf_ref_init(&event_class
->ref_count
);
59 event_class
->name
= g_quark_from_string(name
);
64 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class
*event_class
)
66 const char *name
= NULL
;
72 name
= g_quark_to_string(event_class
->name
);
77 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class
*event_class
)
81 if (!event_class
|| !event_class
->id_set
) {
86 ret
= (int64_t) event_class
->id
;
91 int bt_ctf_event_class_set_id(struct bt_ctf_event_class
*event_class
,
101 if (event_class
->stream_class
) {
103 * We don't allow changing the id if the event class has already
104 * been added to a stream class.
110 event_class
->id
= id
;
111 event_class
->id_set
= 1;
116 struct bt_ctf_stream_class
*bt_ctf_event_class_get_stream_class(
117 struct bt_ctf_event_class
*event_class
)
119 struct bt_ctf_stream_class
*stream_class
= NULL
;
125 stream_class
= event_class
->stream_class
;
126 bt_ctf_stream_class_get(stream_class
);
131 int bt_ctf_event_class_add_field(struct bt_ctf_event_class
*event_class
,
132 struct bt_ctf_field_type
*type
,
137 if (!event_class
|| !type
|| bt_ctf_validate_identifier(name
) ||
138 event_class
->frozen
) {
143 if (!event_class
->fields
) {
144 event_class
->fields
= bt_ctf_field_type_structure_create();
145 if (!event_class
->fields
) {
151 ret
= bt_ctf_field_type_structure_add_field(event_class
->fields
,
157 int bt_ctf_event_class_get_field_count(
158 struct bt_ctf_event_class
*event_class
)
167 ret
= bt_ctf_field_type_structure_get_field_count(event_class
->fields
);
172 int bt_ctf_event_class_get_field(struct bt_ctf_event_class
*event_class
,
173 const char **field_name
, struct bt_ctf_field_type
**field_type
,
178 if (!event_class
|| index
< 0) {
183 ret
= bt_ctf_field_type_structure_get_field(event_class
->fields
,
184 field_name
, field_type
, index
);
189 struct bt_ctf_field_type
*bt_ctf_event_class_get_field_by_name(
190 struct bt_ctf_event_class
*event_class
, const char *name
)
193 struct bt_ctf_field_type
*field_type
= NULL
;
195 if (!event_class
|| !name
) {
199 name_quark
= g_quark_try_string(name
);
205 * No need to increment field_type's reference count since getting it
206 * from the structure already does.
208 field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
209 event_class
->fields
, name
);
214 struct bt_ctf_field_type
*bt_ctf_event_class_get_context_type(
215 struct bt_ctf_event_class
*event_class
)
217 struct bt_ctf_field_type
*context_type
= NULL
;
219 if (!event_class
|| !event_class
->context
) {
223 bt_ctf_field_type_get(event_class
->context
);
224 context_type
= event_class
->context
;
229 int bt_ctf_event_class_set_context_type(
230 struct bt_ctf_event_class
*event_class
,
231 struct bt_ctf_field_type
*context
)
235 if (!event_class
|| !context
|| event_class
->frozen
) {
240 if (bt_ctf_field_type_get_type_id(context
) != CTF_TYPE_STRUCT
) {
245 bt_ctf_field_type_get(context
);
246 bt_ctf_field_type_put(event_class
->context
);
247 event_class
->context
= context
;
253 void bt_ctf_event_class_get(struct bt_ctf_event_class
*event_class
)
259 bt_ctf_ref_get(&event_class
->ref_count
);
262 void bt_ctf_event_class_put(struct bt_ctf_event_class
*event_class
)
268 bt_ctf_ref_put(&event_class
->ref_count
, bt_ctf_event_class_destroy
);
271 struct bt_ctf_event
*bt_ctf_event_create(struct bt_ctf_event_class
*event_class
)
273 struct bt_ctf_event
*event
= NULL
;
279 event
= g_new0(struct bt_ctf_event
, 1);
284 bt_ctf_ref_init(&event
->ref_count
);
285 bt_ctf_event_class_get(event_class
);
286 bt_ctf_event_class_freeze(event_class
);
287 event
->event_class
= event_class
;
288 if (event_class
->context
) {
289 event
->context_payload
= bt_ctf_field_create(
290 event_class
->context
);
292 event
->fields_payload
= bt_ctf_field_create(event_class
->fields
);
297 struct bt_ctf_event_class
*bt_ctf_event_get_class(struct bt_ctf_event
*event
)
299 struct bt_ctf_event_class
*event_class
= NULL
;
305 event_class
= event
->event_class
;
306 bt_ctf_event_class_get(event_class
);
311 struct bt_ctf_clock
*bt_ctf_event_get_clock(struct bt_ctf_event
*event
)
313 struct bt_ctf_clock
*clock
= NULL
;
314 struct bt_ctf_event_class
*event_class
;
315 struct bt_ctf_stream_class
*stream_class
;
321 event_class
= bt_ctf_event_get_class(event
);
326 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
328 goto error_put_event_class
;
331 clock
= bt_ctf_stream_class_get_clock(stream_class
);
333 goto error_put_stream_class
;
336 error_put_stream_class
:
337 bt_ctf_stream_class_put(stream_class
);
338 error_put_event_class
:
339 bt_ctf_event_class_put(event_class
);
344 int bt_ctf_event_set_payload(struct bt_ctf_event
*event
,
346 struct bt_ctf_field
*value
)
350 if (!event
|| !value
|| bt_ctf_validate_identifier(name
)) {
355 ret
= bt_ctf_field_structure_set_field(event
->fields_payload
,
362 struct bt_ctf_field
*bt_ctf_event_get_payload(struct bt_ctf_event
*event
,
365 struct bt_ctf_field
*field
= NULL
;
367 if (!event
|| !name
) {
371 field
= bt_ctf_field_structure_get_field(event
->fields_payload
, name
);
376 struct bt_ctf_field
*bt_ctf_event_get_payload_by_index(
377 struct bt_ctf_event
*event
, int index
)
379 struct bt_ctf_field
*field
= NULL
;
381 if (!event
|| index
< 0) {
385 field
= bt_ctf_field_structure_get_field_by_index(event
->fields_payload
,
391 struct bt_ctf_field
*bt_ctf_event_get_event_context(
392 struct bt_ctf_event
*event
)
394 struct bt_ctf_field
*context
= NULL
;
396 if (!event
|| !event
->context_payload
) {
400 context
= event
->context_payload
;
401 bt_ctf_field_get(context
);
406 int bt_ctf_event_set_event_context(struct bt_ctf_event
*event
,
407 struct bt_ctf_field
*context
)
410 struct bt_ctf_field_type
*field_type
= NULL
;
412 if (!event
|| !context
) {
417 field_type
= bt_ctf_field_get_type(context
);
418 if (field_type
!= event
->event_class
->context
) {
423 bt_ctf_field_get(context
);
424 bt_ctf_field_put(event
->context_payload
);
425 event
->context_payload
= context
;
428 bt_ctf_field_type_put(field_type
);
433 void bt_ctf_event_get(struct bt_ctf_event
*event
)
439 bt_ctf_ref_get(&event
->ref_count
);
442 void bt_ctf_event_put(struct bt_ctf_event
*event
)
448 bt_ctf_ref_put(&event
->ref_count
, bt_ctf_event_destroy
);
452 void bt_ctf_event_class_destroy(struct bt_ctf_ref
*ref
)
454 struct bt_ctf_event_class
*event_class
;
461 * Don't call put() on the stream class. See comment in
462 * bt_ctf_event_class_set_stream_class for explanation.
464 event_class
= container_of(ref
, struct bt_ctf_event_class
, ref_count
);
465 if (event_class
->context
) {
466 bt_ctf_field_type_put(event_class
->context
);
468 if (event_class
->fields
) {
469 bt_ctf_field_type_put(event_class
->fields
);
475 void bt_ctf_event_destroy(struct bt_ctf_ref
*ref
)
477 struct bt_ctf_event
*event
;
483 event
= container_of(ref
, struct bt_ctf_event
,
485 if (event
->event_class
) {
486 bt_ctf_event_class_put(event
->event_class
);
488 if (event
->context_payload
) {
489 bt_ctf_field_put(event
->context_payload
);
491 if (event
->fields_payload
) {
492 bt_ctf_field_put(event
->fields_payload
);
498 void bt_ctf_event_class_freeze(struct bt_ctf_event_class
*event_class
)
501 event_class
->frozen
= 1;
502 bt_ctf_field_type_freeze(event_class
->context
);
503 bt_ctf_field_type_freeze(event_class
->fields
);
507 int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class
*event_class
,
508 struct bt_ctf_stream_class
*stream_class
)
517 /* Allow a NULL stream_class to unset the current stream_class */
518 if (stream_class
&& event_class
->stream_class
) {
523 event_class
->stream_class
= stream_class
;
525 * We don't get() the stream_class since doing so would introduce
526 * a circular ownership between event classes and stream classes.
528 * A stream class will always unset itself from its events before
529 * being destroyed. This ensures that a user won't get a pointer
530 * to a stale stream class instance from an event class.
537 int bt_ctf_event_class_serialize(struct bt_ctf_event_class
*event_class
,
538 struct metadata_context
*context
)
545 stream_id
= bt_ctf_stream_class_get_id(event_class
->stream_class
);
551 context
->current_indentation_level
= 1;
552 g_string_assign(context
->field_name
, "");
553 g_string_append_printf(context
->string
, "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRId64
";\n",
554 g_quark_to_string(event_class
->name
),
558 if (event_class
->context
) {
559 g_string_append(context
->string
, "\tcontext := ");
560 ret
= bt_ctf_field_type_serialize(event_class
->context
,
565 g_string_append(context
->string
, ";\n");
568 if (event_class
->fields
) {
569 g_string_append(context
->string
, "\tfields := ");
570 ret
= bt_ctf_field_type_serialize(event_class
->fields
, context
);
574 g_string_append(context
->string
, ";\n");
577 g_string_append(context
->string
, "};\n\n");
579 context
->current_indentation_level
= 0;
584 int bt_ctf_event_validate(struct bt_ctf_event
*event
)
586 /* Make sure each field's payload has been set */
590 ret
= bt_ctf_field_validate(event
->fields_payload
);
595 if (event
->event_class
->context
) {
596 ret
= bt_ctf_field_validate(event
->context_payload
);
603 int bt_ctf_event_serialize(struct bt_ctf_event
*event
,
604 struct ctf_stream_pos
*pos
)
610 if (event
->context_payload
) {
611 ret
= bt_ctf_field_serialize(event
->context_payload
, pos
);
617 if (event
->fields_payload
) {
618 ret
= bt_ctf_field_serialize(event
->fields_payload
, pos
);
628 int bt_ctf_event_set_timestamp(struct bt_ctf_event
*event
,
634 if (event
->timestamp
) {
639 event
->timestamp
= timestamp
;
645 uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event
*event
)
648 return event
->timestamp
;