Commit | Line | Data |
---|---|---|
273b65be JG |
1 | /* |
2 | * event.c | |
3 | * | |
d2dc44b6 | 4 | * Babeltrace CTF IR - Event |
273b65be | 5 | * |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be 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/event.h> | |
30 | #include <babeltrace/ctf-writer/event-types.h> | |
31 | #include <babeltrace/ctf-writer/event-fields.h> | |
adc315b8 JG |
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> | |
2f100782 | 35 | #include <babeltrace/ctf-ir/stream-class.h> |
bc37ae52 | 36 | #include <babeltrace/ctf-ir/trace-internal.h> |
273b65be JG |
37 | #include <babeltrace/compiler.h> |
38 | ||
39 | static | |
40 | void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref); | |
41 | static | |
42 | void bt_ctf_event_destroy(struct bt_ctf_ref *ref); | |
43 | ||
44 | struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name) | |
45 | { | |
46 | struct bt_ctf_event_class *event_class = NULL; | |
47 | ||
48 | if (validate_identifier(name)) { | |
49 | goto end; | |
50 | } | |
51 | ||
52 | event_class = g_new0(struct bt_ctf_event_class, 1); | |
53 | if (!event_class) { | |
54 | goto end; | |
55 | } | |
56 | ||
57 | bt_ctf_ref_init(&event_class->ref_count); | |
58 | event_class->name = g_quark_from_string(name); | |
59 | end: | |
60 | return event_class; | |
61 | } | |
62 | ||
2f100782 JG |
63 | const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class) |
64 | { | |
65 | const char *name = NULL; | |
66 | ||
67 | if (!event_class) { | |
68 | goto end; | |
69 | } | |
70 | ||
71 | name = g_quark_to_string(event_class->name); | |
72 | end: | |
73 | return name; | |
74 | } | |
75 | ||
76 | int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class) | |
77 | { | |
78 | int64_t ret; | |
79 | ||
80 | if (!event_class || !event_class->id_set) { | |
81 | ret = -1; | |
82 | goto end; | |
83 | } | |
84 | ||
85 | ret = (int64_t) event_class->id; | |
86 | end: | |
87 | return ret; | |
88 | } | |
89 | ||
90 | int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class, | |
91 | uint32_t id) | |
92 | { | |
93 | int ret = 0; | |
94 | ||
95 | if (!event_class) { | |
96 | ret = -1; | |
97 | goto end; | |
98 | } | |
99 | ||
100 | if (event_class->stream_class) { | |
101 | /* | |
102 | * We don't allow changing the id if the event class has already | |
103 | * been added to a stream class. | |
104 | */ | |
105 | ret = -1; | |
106 | goto end; | |
107 | } | |
108 | ||
109 | event_class->id = id; | |
110 | event_class->id_set = 1; | |
111 | end: | |
112 | return ret; | |
113 | } | |
114 | ||
115 | struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class( | |
116 | struct bt_ctf_event_class *event_class) | |
117 | { | |
118 | struct bt_ctf_stream_class *stream_class = NULL; | |
119 | ||
120 | if (!event_class) { | |
121 | goto end; | |
122 | } | |
123 | ||
124 | stream_class = event_class->stream_class; | |
125 | bt_ctf_stream_class_get(stream_class); | |
126 | end: | |
127 | return stream_class; | |
128 | } | |
129 | ||
273b65be JG |
130 | int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, |
131 | struct bt_ctf_field_type *type, | |
132 | const char *name) | |
133 | { | |
134 | int ret = 0; | |
135 | ||
136 | if (!event_class || !type || validate_identifier(name) || | |
137 | event_class->frozen) { | |
138 | ret = -1; | |
139 | goto end; | |
140 | } | |
141 | ||
142 | if (!event_class->fields) { | |
143 | event_class->fields = bt_ctf_field_type_structure_create(); | |
144 | if (!event_class->fields) { | |
145 | ret = -1; | |
146 | goto end; | |
147 | } | |
148 | } | |
149 | ||
150 | ret = bt_ctf_field_type_structure_add_field(event_class->fields, | |
151 | type, name); | |
152 | end: | |
153 | return ret; | |
154 | } | |
155 | ||
074ee56d | 156 | int bt_ctf_event_class_get_field_count( |
2f100782 JG |
157 | struct bt_ctf_event_class *event_class) |
158 | { | |
074ee56d | 159 | int ret; |
2f100782 JG |
160 | |
161 | if (!event_class) { | |
162 | ret = -1; | |
163 | goto end; | |
164 | } | |
165 | ||
166 | ret = bt_ctf_field_type_structure_get_field_count(event_class->fields); | |
167 | end: | |
168 | return ret; | |
169 | } | |
170 | ||
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, | |
074ee56d | 173 | int index) |
2f100782 JG |
174 | { |
175 | int ret; | |
176 | ||
074ee56d | 177 | if (!event_class || index < 0) { |
2f100782 JG |
178 | ret = -1; |
179 | goto end; | |
180 | } | |
181 | ||
182 | ret = bt_ctf_field_type_structure_get_field(event_class->fields, | |
183 | field_name, field_type, index); | |
184 | end: | |
185 | return ret; | |
186 | } | |
187 | ||
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) | |
190 | { | |
191 | GQuark name_quark; | |
192 | struct bt_ctf_field_type *field_type = NULL; | |
193 | ||
194 | if (!event_class || !name) { | |
195 | goto end; | |
196 | } | |
197 | ||
198 | name_quark = g_quark_try_string(name); | |
199 | if (!name_quark) { | |
200 | goto end; | |
201 | } | |
202 | ||
203 | /* | |
204 | * No need to increment field_type's reference count since getting it | |
205 | * from the structure already does. | |
206 | */ | |
207 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
208 | event_class->fields, name); | |
209 | end: | |
210 | return field_type; | |
211 | } | |
212 | ||
273b65be JG |
213 | void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class) |
214 | { | |
215 | if (!event_class) { | |
216 | return; | |
217 | } | |
218 | ||
219 | bt_ctf_ref_get(&event_class->ref_count); | |
220 | } | |
221 | ||
222 | void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class) | |
223 | { | |
224 | if (!event_class) { | |
225 | return; | |
226 | } | |
227 | ||
228 | bt_ctf_ref_put(&event_class->ref_count, bt_ctf_event_class_destroy); | |
229 | } | |
230 | ||
231 | struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class) | |
232 | { | |
233 | struct bt_ctf_event *event = NULL; | |
234 | ||
235 | if (!event_class) { | |
236 | goto end; | |
237 | } | |
238 | ||
239 | event = g_new0(struct bt_ctf_event, 1); | |
240 | if (!event) { | |
241 | goto end; | |
242 | } | |
243 | ||
244 | bt_ctf_ref_init(&event->ref_count); | |
245 | bt_ctf_event_class_get(event_class); | |
246 | bt_ctf_event_class_freeze(event_class); | |
247 | event->event_class = event_class; | |
248 | event->context_payload = bt_ctf_field_create(event_class->context); | |
249 | event->fields_payload = bt_ctf_field_create(event_class->fields); | |
250 | end: | |
251 | return event; | |
252 | } | |
253 | ||
2f100782 JG |
254 | struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event) |
255 | { | |
256 | struct bt_ctf_event_class *event_class = NULL; | |
257 | ||
258 | if (!event) { | |
259 | goto end; | |
260 | } | |
261 | ||
262 | event_class = event->event_class; | |
263 | bt_ctf_event_class_get(event_class); | |
264 | end: | |
265 | return event_class; | |
266 | } | |
267 | ||
268 | struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event) | |
269 | { | |
270 | struct bt_ctf_clock *clock = NULL; | |
271 | struct bt_ctf_event_class *event_class; | |
272 | struct bt_ctf_stream_class *stream_class; | |
273 | ||
274 | if (!event) { | |
275 | goto end; | |
276 | } | |
277 | ||
278 | event_class = bt_ctf_event_get_class(event); | |
279 | if (!event_class) { | |
280 | goto end; | |
281 | } | |
282 | ||
283 | stream_class = bt_ctf_event_class_get_stream_class(event_class); | |
284 | if (!stream_class) { | |
285 | goto error_put_event_class; | |
286 | } | |
287 | ||
288 | clock = bt_ctf_stream_class_get_clock(stream_class); | |
289 | if (!clock) { | |
290 | goto error_put_stream_class; | |
291 | } | |
292 | ||
293 | error_put_stream_class: | |
294 | bt_ctf_stream_class_put(stream_class); | |
295 | error_put_event_class: | |
296 | bt_ctf_event_class_put(event_class); | |
297 | end: | |
298 | return clock; | |
299 | } | |
300 | ||
273b65be JG |
301 | int bt_ctf_event_set_payload(struct bt_ctf_event *event, |
302 | const char *name, | |
303 | struct bt_ctf_field *value) | |
304 | { | |
305 | int ret = 0; | |
306 | ||
307 | if (!event || !value || validate_identifier(name)) { | |
308 | ret = -1; | |
309 | goto end; | |
310 | } | |
311 | ||
312 | ret = bt_ctf_field_structure_set_field(event->fields_payload, | |
313 | name, value); | |
314 | end: | |
315 | return ret; | |
316 | } | |
317 | ||
318 | ||
319 | struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, | |
320 | const char *name) | |
321 | { | |
322 | struct bt_ctf_field *field = NULL; | |
323 | ||
324 | if (!event || !name) { | |
325 | goto end; | |
326 | } | |
327 | ||
328 | field = bt_ctf_field_structure_get_field(event->fields_payload, name); | |
329 | end: | |
330 | return field; | |
331 | } | |
332 | ||
2f100782 | 333 | struct bt_ctf_field *bt_ctf_event_get_payload_by_index( |
074ee56d | 334 | struct bt_ctf_event *event, int index) |
2f100782 JG |
335 | { |
336 | struct bt_ctf_field *field = NULL; | |
337 | ||
074ee56d | 338 | if (!event || index < 0) { |
2f100782 JG |
339 | goto end; |
340 | } | |
341 | ||
342 | field = bt_ctf_field_structure_get_field_by_index(event->fields_payload, | |
343 | index); | |
344 | end: | |
345 | return field; | |
346 | } | |
347 | ||
273b65be JG |
348 | void bt_ctf_event_get(struct bt_ctf_event *event) |
349 | { | |
350 | if (!event) { | |
351 | return; | |
352 | } | |
353 | ||
354 | bt_ctf_ref_get(&event->ref_count); | |
355 | } | |
356 | ||
357 | void bt_ctf_event_put(struct bt_ctf_event *event) | |
358 | { | |
359 | if (!event) { | |
360 | return; | |
361 | } | |
362 | ||
363 | bt_ctf_ref_put(&event->ref_count, bt_ctf_event_destroy); | |
364 | } | |
365 | ||
366 | static | |
367 | void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref) | |
368 | { | |
369 | struct bt_ctf_event_class *event_class; | |
370 | ||
371 | if (!ref) { | |
372 | return; | |
373 | } | |
374 | ||
2f100782 JG |
375 | /* |
376 | * Don't call put() on the stream class. See comment in | |
377 | * bt_ctf_event_class_set_stream_class for explanation. | |
378 | */ | |
273b65be JG |
379 | event_class = container_of(ref, struct bt_ctf_event_class, ref_count); |
380 | bt_ctf_field_type_put(event_class->context); | |
381 | bt_ctf_field_type_put(event_class->fields); | |
382 | g_free(event_class); | |
383 | } | |
384 | ||
385 | static | |
386 | void bt_ctf_event_destroy(struct bt_ctf_ref *ref) | |
387 | { | |
388 | struct bt_ctf_event *event; | |
389 | ||
390 | if (!ref) { | |
391 | return; | |
392 | } | |
393 | ||
394 | event = container_of(ref, struct bt_ctf_event, | |
395 | ref_count); | |
396 | bt_ctf_event_class_put(event->event_class); | |
397 | bt_ctf_field_put(event->context_payload); | |
398 | bt_ctf_field_put(event->fields_payload); | |
399 | g_free(event); | |
400 | } | |
401 | ||
402 | BT_HIDDEN | |
403 | void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class) | |
404 | { | |
405 | assert(event_class); | |
406 | event_class->frozen = 1; | |
407 | bt_ctf_field_type_freeze(event_class->context); | |
408 | bt_ctf_field_type_freeze(event_class->fields); | |
409 | } | |
410 | ||
411 | BT_HIDDEN | |
2f100782 JG |
412 | int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class *event_class, |
413 | struct bt_ctf_stream_class *stream_class) | |
273b65be JG |
414 | { |
415 | int ret = 0; | |
416 | ||
2f100782 | 417 | if (!event_class) { |
273b65be JG |
418 | ret = -1; |
419 | goto end; | |
420 | } | |
421 | ||
2f100782 JG |
422 | /* Allow a NULL stream_class to unset the current stream_class */ |
423 | if (stream_class && event_class->stream_class) { | |
273b65be JG |
424 | ret = -1; |
425 | goto end; | |
426 | } | |
427 | ||
2f100782 JG |
428 | event_class->stream_class = stream_class; |
429 | /* | |
430 | * We don't get() the stream_class since doing so would introduce | |
431 | * a circular ownership between event classes and stream classes. | |
432 | * | |
433 | * A stream class will always unset itself from its events before | |
434 | * being destroyed. This ensures that a user won't get a pointer | |
435 | * to a stale stream class instance from an event class. | |
436 | */ | |
273b65be JG |
437 | end: |
438 | return ret; | |
439 | } | |
440 | ||
441 | BT_HIDDEN | |
442 | int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class, | |
443 | struct metadata_context *context) | |
444 | { | |
445 | int ret = 0; | |
2f100782 | 446 | int64_t stream_id; |
273b65be JG |
447 | |
448 | assert(event_class); | |
449 | assert(context); | |
2f100782 JG |
450 | stream_id = bt_ctf_stream_class_get_id(event_class->stream_class); |
451 | if (stream_id < 0) { | |
452 | ret = -1; | |
453 | goto end; | |
454 | } | |
455 | ||
273b65be JG |
456 | context->current_indentation_level = 1; |
457 | g_string_assign(context->field_name, ""); | |
2f100782 | 458 | g_string_append_printf(context->string, "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRId64 ";\n", |
273b65be JG |
459 | g_quark_to_string(event_class->name), |
460 | event_class->id, | |
2f100782 | 461 | stream_id); |
273b65be JG |
462 | |
463 | if (event_class->context) { | |
464 | g_string_append(context->string, "\tcontext := "); | |
465 | ret = bt_ctf_field_type_serialize(event_class->context, | |
466 | context); | |
467 | if (ret) { | |
468 | goto end; | |
469 | } | |
470 | g_string_append(context->string, ";\n"); | |
471 | } | |
472 | ||
473 | if (event_class->fields) { | |
474 | g_string_append(context->string, "\tfields := "); | |
475 | ret = bt_ctf_field_type_serialize(event_class->fields, context); | |
476 | if (ret) { | |
477 | goto end; | |
478 | } | |
479 | g_string_append(context->string, ";\n"); | |
480 | } | |
481 | ||
482 | g_string_append(context->string, "};\n\n"); | |
483 | end: | |
484 | context->current_indentation_level = 0; | |
485 | return ret; | |
486 | } | |
487 | ||
488 | BT_HIDDEN | |
489 | int bt_ctf_event_validate(struct bt_ctf_event *event) | |
490 | { | |
491 | /* Make sure each field's payload has been set */ | |
492 | int ret; | |
493 | ||
494 | assert(event); | |
495 | ret = bt_ctf_field_validate(event->fields_payload); | |
496 | if (ret) { | |
497 | goto end; | |
498 | } | |
499 | ||
500 | if (event->event_class->context) { | |
501 | ret = bt_ctf_field_validate(event->context_payload); | |
502 | } | |
503 | end: | |
504 | return ret; | |
505 | } | |
506 | ||
507 | BT_HIDDEN | |
508 | int bt_ctf_event_serialize(struct bt_ctf_event *event, | |
509 | struct ctf_stream_pos *pos) | |
510 | { | |
511 | int ret = 0; | |
512 | ||
513 | assert(event); | |
514 | assert(pos); | |
515 | if (event->context_payload) { | |
516 | ret = bt_ctf_field_serialize(event->context_payload, pos); | |
517 | if (ret) { | |
518 | goto end; | |
519 | } | |
520 | } | |
521 | ||
522 | if (event->fields_payload) { | |
523 | ret = bt_ctf_field_serialize(event->fields_payload, pos); | |
524 | if (ret) { | |
525 | goto end; | |
526 | } | |
527 | } | |
528 | end: | |
529 | return ret; | |
530 | } | |
531 | ||
532 | BT_HIDDEN | |
533 | int bt_ctf_event_set_timestamp(struct bt_ctf_event *event, | |
534 | uint64_t timestamp) | |
535 | { | |
536 | int ret = 0; | |
537 | ||
538 | assert(event); | |
539 | if (event->timestamp) { | |
540 | ret = -1; | |
541 | goto end; | |
542 | } | |
543 | ||
544 | event->timestamp = timestamp; | |
545 | end: | |
546 | return ret; | |
547 | } | |
548 | ||
549 | BT_HIDDEN | |
550 | uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event *event) | |
551 | { | |
552 | assert(event); | |
553 | return event->timestamp; | |
554 | } |