Commit | Line | Data |
---|---|---|
273b65be | 1 | /* |
f2b0325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
de9dd397 | 3 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
4 | * |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
7b010242 PP |
24 | #define BT_LOG_TAG "EVENT" |
25 | #include <babeltrace/lib-logging-internal.h> | |
26 | ||
8deee039 | 27 | #include <babeltrace/assert-pre-internal.h> |
0f15f666 PP |
28 | #include <babeltrace/trace-ir/field-internal.h> |
29 | #include <babeltrace/trace-ir/field-class-internal.h> | |
108b91d0 | 30 | #include <babeltrace/trace-ir/clock-class.h> |
78cf9df6 | 31 | #include <babeltrace/trace-ir/clock-value-const.h> |
108b91d0 PP |
32 | #include <babeltrace/trace-ir/clock-value-internal.h> |
33 | #include <babeltrace/trace-ir/clock-class-internal.h> | |
78cf9df6 | 34 | #include <babeltrace/trace-ir/event-const.h> |
108b91d0 PP |
35 | #include <babeltrace/trace-ir/event-internal.h> |
36 | #include <babeltrace/trace-ir/event-class.h> | |
37 | #include <babeltrace/trace-ir/event-class-internal.h> | |
38 | #include <babeltrace/trace-ir/stream-class.h> | |
78cf9df6 | 39 | #include <babeltrace/trace-ir/stream-class-const.h> |
108b91d0 PP |
40 | #include <babeltrace/trace-ir/stream-class-internal.h> |
41 | #include <babeltrace/trace-ir/stream-internal.h> | |
42 | #include <babeltrace/trace-ir/packet.h> | |
43 | #include <babeltrace/trace-ir/packet-internal.h> | |
44 | #include <babeltrace/trace-ir/trace.h> | |
45 | #include <babeltrace/trace-ir/trace-internal.h> | |
46 | #include <babeltrace/trace-ir/packet-internal.h> | |
108b91d0 | 47 | #include <babeltrace/trace-ir/attributes-internal.h> |
8b45963b | 48 | #include <babeltrace/assert-internal.h> |
8c6884d9 | 49 | #include <babeltrace/compiler-internal.h> |
7b010242 | 50 | #include <inttypes.h> |
273b65be | 51 | |
8deee039 | 52 | BT_HIDDEN |
78cf9df6 | 53 | void _bt_event_set_is_frozen(const struct bt_event *event, bool is_frozen) |
8deee039 | 54 | { |
8deee039 | 55 | BT_ASSERT(event); |
7b33a0e0 PP |
56 | BT_LIB_LOGD("Setting event's frozen state: %!+e, is-frozen=%d", |
57 | event, is_frozen); | |
a6918753 PP |
58 | |
59 | if (event->header_field) { | |
7b33a0e0 PP |
60 | BT_LOGD_STR("Setting event's header field's frozen state."); |
61 | bt_field_set_is_frozen( | |
c5a24b0a | 62 | event->header_field->field, is_frozen); |
a6918753 PP |
63 | } |
64 | ||
7b33a0e0 PP |
65 | if (event->common_context_field) { |
66 | BT_LOGD_STR("Setting event's common context field's frozen state."); | |
67 | bt_field_set_is_frozen( | |
68 | event->common_context_field, is_frozen); | |
a6918753 PP |
69 | } |
70 | ||
7b33a0e0 PP |
71 | if (event->specific_context_field) { |
72 | BT_LOGD_STR("Setting event's specific context field's frozen state."); | |
73 | bt_field_set_is_frozen(event->specific_context_field, | |
c5a24b0a | 74 | is_frozen); |
a6918753 PP |
75 | } |
76 | ||
77 | if (event->payload_field) { | |
7b33a0e0 PP |
78 | BT_LOGD_STR("Setting event's payload field's frozen state."); |
79 | bt_field_set_is_frozen(event->payload_field, | |
c5a24b0a | 80 | is_frozen); |
a6918753 PP |
81 | } |
82 | ||
78cf9df6 | 83 | ((struct bt_event *) event)->frozen = is_frozen; |
7b33a0e0 | 84 | BT_LOGD_STR("Setting event's packet's frozen state."); |
18acc6f8 | 85 | bt_packet_set_is_frozen(event->packet, is_frozen); |
8deee039 PP |
86 | } |
87 | ||
a6918753 | 88 | static |
7b33a0e0 | 89 | void recycle_event_header_field(struct bt_field_wrapper *field_wrapper, |
a6918753 PP |
90 | struct bt_stream_class *stream_class) |
91 | { | |
92 | BT_ASSERT(field_wrapper); | |
93 | BT_LIB_LOGD("Recycling event header field: " | |
94 | "addr=%p, %![sc-]+S, %![field-]+f", field_wrapper, | |
95 | stream_class, field_wrapper->field); | |
96 | bt_object_pool_recycle_object( | |
97 | &stream_class->event_header_field_pool, | |
98 | field_wrapper); | |
99 | } | |
100 | ||
7b33a0e0 | 101 | static inline |
a6918753 | 102 | struct bt_field_wrapper *create_event_header_field( |
7b33a0e0 | 103 | struct bt_stream_class *stream_class) |
a6918753 PP |
104 | { |
105 | struct bt_field_wrapper *field_wrapper = NULL; | |
106 | ||
107 | field_wrapper = bt_field_wrapper_create( | |
7b33a0e0 | 108 | &stream_class->event_header_field_pool, |
78cf9df6 | 109 | stream_class->event_header_fc); |
a6918753 PP |
110 | if (!field_wrapper) { |
111 | goto error; | |
112 | } | |
113 | ||
114 | goto end; | |
115 | ||
116 | error: | |
117 | if (field_wrapper) { | |
7b33a0e0 | 118 | recycle_event_header_field(field_wrapper, stream_class); |
a6918753 PP |
119 | field_wrapper = NULL; |
120 | } | |
121 | ||
122 | end: | |
123 | return field_wrapper; | |
124 | } | |
125 | ||
126 | BT_HIDDEN | |
127 | struct bt_event *bt_event_new(struct bt_event_class *event_class) | |
8deee039 | 128 | { |
8deee039 | 129 | struct bt_event *event = NULL; |
a6918753 | 130 | struct bt_stream_class *stream_class; |
939190b3 | 131 | struct bt_field_class *fc; |
8deee039 | 132 | |
7b33a0e0 | 133 | BT_ASSERT(event_class); |
8deee039 PP |
134 | event = g_new0(struct bt_event, 1); |
135 | if (!event) { | |
136 | BT_LOGE_STR("Failed to allocate one event."); | |
137 | goto error; | |
138 | } | |
139 | ||
7b33a0e0 | 140 | bt_object_init_unique(&event->base); |
a6918753 PP |
141 | stream_class = bt_event_class_borrow_stream_class(event_class); |
142 | BT_ASSERT(stream_class); | |
7b33a0e0 | 143 | |
78cf9df6 | 144 | if (stream_class->event_header_fc) { |
7b33a0e0 PP |
145 | event->header_field = create_event_header_field(stream_class); |
146 | if (!event->header_field) { | |
147 | BT_LOGE_STR("Cannot create event header field."); | |
148 | goto error; | |
149 | } | |
150 | } | |
151 | ||
78cf9df6 | 152 | fc = stream_class->event_common_context_fc; |
939190b3 PP |
153 | if (fc) { |
154 | event->common_context_field = bt_field_create(fc); | |
7b33a0e0 PP |
155 | if (!event->common_context_field) { |
156 | /* bt_field_create() logs errors */ | |
157 | goto error; | |
158 | } | |
159 | } | |
160 | ||
78cf9df6 | 161 | fc = event_class->specific_context_fc; |
939190b3 PP |
162 | if (fc) { |
163 | event->specific_context_field = bt_field_create(fc); | |
7b33a0e0 PP |
164 | if (!event->specific_context_field) { |
165 | /* bt_field_create() logs errors */ | |
166 | goto error; | |
167 | } | |
168 | } | |
169 | ||
78cf9df6 | 170 | fc = event_class->payload_fc; |
939190b3 PP |
171 | if (fc) { |
172 | event->payload_field = bt_field_create(fc); | |
7b33a0e0 PP |
173 | if (!event->payload_field) { |
174 | /* bt_field_create() logs errors */ | |
175 | goto error; | |
176 | } | |
177 | } | |
178 | ||
179 | if (stream_class->default_clock_class) { | |
180 | event->default_cv = bt_clock_value_create( | |
181 | stream_class->default_clock_class); | |
182 | if (!event->default_cv) { | |
183 | /* bt_clock_value_create() logs errors */ | |
184 | goto error; | |
185 | } | |
a6918753 PP |
186 | } |
187 | ||
8deee039 PP |
188 | goto end; |
189 | ||
190 | error: | |
a6918753 PP |
191 | if (event) { |
192 | bt_event_destroy(event); | |
193 | event = NULL; | |
194 | } | |
195 | ||
196 | end: | |
197 | return event; | |
198 | } | |
199 | ||
5fe68922 | 200 | struct bt_event_class *bt_event_borrow_class(struct bt_event *event) |
2f100782 | 201 | { |
8b45963b | 202 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
18acc6f8 | 203 | return event->class; |
2f100782 JG |
204 | } |
205 | ||
78cf9df6 PP |
206 | const struct bt_event_class *bt_event_borrow_class_const( |
207 | const struct bt_event *event) | |
208 | { | |
209 | return bt_event_borrow_class((void *) event); | |
210 | } | |
211 | ||
8b45963b | 212 | struct bt_stream *bt_event_borrow_stream(struct bt_event *event) |
8b45963b PP |
213 | { |
214 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
5fe68922 | 215 | return event->packet ? event->packet->stream : NULL; |
8b45963b PP |
216 | } |
217 | ||
78cf9df6 PP |
218 | const struct bt_stream *bt_event_borrow_stream_const( |
219 | const struct bt_event *event) | |
220 | { | |
221 | return bt_event_borrow_stream((void *) event); | |
222 | } | |
223 | ||
7b33a0e0 | 224 | struct bt_field *bt_event_borrow_header_field(struct bt_event *event) |
e5e6eb3a | 225 | { |
18acc6f8 | 226 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
7b33a0e0 | 227 | return event->header_field ? event->header_field->field : NULL; |
e5e6eb3a JG |
228 | } |
229 | ||
78cf9df6 PP |
230 | const struct bt_field *bt_event_borrow_header_field_const( |
231 | const struct bt_event *event) | |
9e550e5f | 232 | { |
78cf9df6 PP |
233 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
234 | return event->header_field ? event->header_field->field : NULL; | |
9e550e5f PP |
235 | } |
236 | ||
7b33a0e0 | 237 | struct bt_field *bt_event_borrow_common_context_field(struct bt_event *event) |
662e778c | 238 | { |
18acc6f8 | 239 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
7b33a0e0 | 240 | return event->common_context_field; |
662e778c JG |
241 | } |
242 | ||
78cf9df6 PP |
243 | const struct bt_field *bt_event_borrow_common_context_field_const( |
244 | const struct bt_event *event) | |
9e550e5f | 245 | { |
78cf9df6 PP |
246 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
247 | return event->common_context_field; | |
9e550e5f PP |
248 | } |
249 | ||
7b33a0e0 | 250 | struct bt_field *bt_event_borrow_specific_context_field(struct bt_event *event) |
f655a84d | 251 | { |
18acc6f8 | 252 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
7b33a0e0 | 253 | return event->specific_context_field; |
f655a84d JG |
254 | } |
255 | ||
78cf9df6 PP |
256 | const struct bt_field *bt_event_borrow_specific_context_field_const( |
257 | const struct bt_event *event) | |
9e550e5f | 258 | { |
78cf9df6 PP |
259 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
260 | return event->specific_context_field; | |
9e550e5f PP |
261 | } |
262 | ||
7b33a0e0 | 263 | struct bt_field *bt_event_borrow_payload_field(struct bt_event *event) |
5fd2e9fd | 264 | { |
18acc6f8 | 265 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
7b33a0e0 | 266 | return event->payload_field; |
5fd2e9fd PP |
267 | } |
268 | ||
78cf9df6 PP |
269 | const struct bt_field *bt_event_borrow_payload_field_const( |
270 | const struct bt_event *event) | |
9e550e5f | 271 | { |
78cf9df6 PP |
272 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
273 | return event->payload_field; | |
9e550e5f PP |
274 | } |
275 | ||
a6918753 PP |
276 | static |
277 | void release_event_header_field(struct bt_field_wrapper *field_wrapper, | |
18acc6f8 | 278 | struct bt_event *event) |
273b65be | 279 | { |
7b33a0e0 | 280 | if (!event->class) { |
a6918753 PP |
281 | bt_field_wrapper_destroy(field_wrapper); |
282 | } else { | |
283 | struct bt_stream_class *stream_class = | |
7b33a0e0 | 284 | bt_event_class_borrow_stream_class(event->class); |
a6918753 PP |
285 | |
286 | BT_ASSERT(stream_class); | |
7b33a0e0 | 287 | recycle_event_header_field(field_wrapper, stream_class); |
a6918753 | 288 | } |
273b65be JG |
289 | } |
290 | ||
7b33a0e0 PP |
291 | BT_HIDDEN |
292 | void bt_event_destroy(struct bt_event *event) | |
18acc6f8 | 293 | { |
7b33a0e0 PP |
294 | BT_ASSERT(event); |
295 | BT_LIB_LOGD("Destroying event: %!+e", event); | |
18acc6f8 PP |
296 | |
297 | if (event->header_field) { | |
298 | BT_LOGD_STR("Releasing event's header field."); | |
7b33a0e0 | 299 | release_event_header_field(event->header_field, event); |
1248f5ea | 300 | event->header_field = NULL; |
18acc6f8 PP |
301 | } |
302 | ||
7b33a0e0 PP |
303 | if (event->common_context_field) { |
304 | BT_LOGD_STR("Destroying event's stream event context field."); | |
305 | bt_field_destroy(event->common_context_field); | |
1248f5ea | 306 | event->common_context_field = NULL; |
18acc6f8 PP |
307 | } |
308 | ||
7b33a0e0 PP |
309 | if (event->specific_context_field) { |
310 | BT_LOGD_STR("Destroying event's context field."); | |
311 | bt_field_destroy(event->specific_context_field); | |
1248f5ea | 312 | event->specific_context_field = NULL; |
18acc6f8 PP |
313 | } |
314 | ||
315 | if (event->payload_field) { | |
7b33a0e0 PP |
316 | BT_LOGD_STR("Destroying event's payload field."); |
317 | bt_field_destroy(event->payload_field); | |
1248f5ea | 318 | event->payload_field = NULL; |
18acc6f8 PP |
319 | } |
320 | ||
7b33a0e0 | 321 | BT_LOGD_STR("Putting event's class."); |
8138bfe1 | 322 | bt_object_put_ref(event->class); |
7b33a0e0 PP |
323 | |
324 | if (event->default_cv) { | |
325 | bt_clock_value_recycle(event->default_cv); | |
1248f5ea | 326 | event->default_cv = NULL; |
18acc6f8 | 327 | } |
18acc6f8 | 328 | |
03039f21 | 329 | BT_LOGD_STR("Putting event's packet."); |
1248f5ea | 330 | BT_OBJECT_PUT_REF_AND_RESET(event->packet); |
273b65be JG |
331 | g_free(event); |
332 | } | |
333 | ||
78cf9df6 PP |
334 | void bt_event_set_default_clock_value(struct bt_event *event, |
335 | uint64_t value_cycles) | |
8fc063a2 | 336 | { |
7b33a0e0 PP |
337 | struct bt_stream_class *sc; |
338 | ||
8fc063a2 | 339 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
7b33a0e0 PP |
340 | BT_ASSERT_PRE_EVENT_HOT(event); |
341 | sc = bt_event_class_borrow_stream_class_inline(event->class); | |
342 | BT_ASSERT(sc); | |
343 | BT_ASSERT_PRE(sc->default_clock_class, | |
344 | "Event's stream class has no default clock class: " | |
345 | "%![ev-]+e, %![sc-]+S", event, sc); | |
346 | BT_ASSERT(event->default_cv); | |
347 | bt_clock_value_set_value_inline(event->default_cv, value_cycles); | |
348 | BT_LIB_LOGV("Set event's default clock value: %![event-]+e, " | |
349 | "value=%" PRIu64, event, value_cycles); | |
8fc063a2 PP |
350 | } |
351 | ||
78cf9df6 PP |
352 | enum bt_clock_value_status bt_event_borrow_default_clock_value_const( |
353 | const struct bt_event *event, | |
354 | const struct bt_clock_value **clock_value) | |
78586d8a | 355 | { |
8b45963b | 356 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
7b33a0e0 PP |
357 | BT_ASSERT_PRE_NON_NULL(clock_value, "Clock value (output)"); |
358 | *clock_value = event->default_cv; | |
359 | return BT_CLOCK_VALUE_STATUS_KNOWN; | |
1556a1af JG |
360 | } |
361 | ||
5fe68922 | 362 | struct bt_packet *bt_event_borrow_packet(struct bt_event *event) |
5c0f40f4 | 363 | { |
8b45963b | 364 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
7b33a0e0 | 365 | return event->packet; |
5c0f40f4 JG |
366 | } |
367 | ||
78cf9df6 PP |
368 | const struct bt_packet *bt_event_borrow_packet_const( |
369 | const struct bt_event *event) | |
9e550e5f | 370 | { |
78cf9df6 | 371 | return bt_event_borrow_packet((void *) event); |
9e550e5f PP |
372 | } |
373 | ||
78cf9df6 PP |
374 | int bt_event_move_header_field(struct bt_event *event, |
375 | struct bt_event_header_field *header_field) | |
a6918753 PP |
376 | { |
377 | struct bt_stream_class *stream_class; | |
78cf9df6 | 378 | struct bt_field_wrapper *field_wrapper = (void *) header_field; |
a6918753 PP |
379 | |
380 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
381 | BT_ASSERT_PRE_NON_NULL(field_wrapper, "Header field"); | |
7b33a0e0 PP |
382 | BT_ASSERT_PRE_EVENT_HOT(event); |
383 | stream_class = bt_event_class_borrow_stream_class_inline(event->class); | |
939190b3 | 384 | BT_ASSERT_PRE(stream_class->event_header_fc, |
66fd07a5 | 385 | "Stream class has no event header field class: %!+S", |
a6918753 PP |
386 | stream_class); |
387 | ||
388 | /* Recycle current header field: always exists */ | |
18acc6f8 | 389 | BT_ASSERT(event->header_field); |
7b33a0e0 | 390 | recycle_event_header_field(event->header_field, stream_class); |
a6918753 PP |
391 | |
392 | /* Move new field */ | |
7b33a0e0 | 393 | event->header_field = field_wrapper; |
a6918753 PP |
394 | return 0; |
395 | } |