lib: remove CTF concepts of packet and event headers
[babeltrace.git] / lib / trace-ir / packet.c
1 /*
2 * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #define BT_LOG_TAG "PACKET"
24 #include <babeltrace/lib-logging-internal.h>
25
26 #include <babeltrace/assert-pre-internal.h>
27 #include <babeltrace/trace-ir/field-internal.h>
28 #include <babeltrace/trace-ir/packet-const.h>
29 #include <babeltrace/trace-ir/packet.h>
30 #include <babeltrace/trace-ir/packet-internal.h>
31 #include <babeltrace/trace-ir/field-wrapper-internal.h>
32 #include <babeltrace/trace-ir/trace.h>
33 #include <babeltrace/trace-ir/stream-class-internal.h>
34 #include <babeltrace/trace-ir/stream-class.h>
35 #include <babeltrace/trace-ir/stream.h>
36 #include <babeltrace/trace-ir/stream-internal.h>
37 #include <babeltrace/trace-ir/clock-snapshot-internal.h>
38 #include <babeltrace/trace-ir/trace-internal.h>
39 #include <babeltrace/object-internal.h>
40 #include <babeltrace/assert-internal.h>
41 #include <inttypes.h>
42
43 #define BT_ASSERT_PRE_PACKET_HOT(_packet) \
44 BT_ASSERT_PRE_HOT((_packet), "Packet", ": %!+a", (_packet))
45
46 struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet)
47 {
48 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
49 return packet->stream;
50 }
51
52 const struct bt_stream *bt_packet_borrow_stream_const(
53 const struct bt_packet *packet)
54 {
55 return bt_packet_borrow_stream((void *) packet);
56 }
57
58 struct bt_field *bt_packet_borrow_context_field(struct bt_packet *packet)
59 {
60 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
61 return packet->context_field ? packet->context_field->field : NULL;
62 }
63
64 const struct bt_field *bt_packet_borrow_context_field_const(
65 const struct bt_packet *packet)
66 {
67 return bt_packet_borrow_context_field((void *) packet);
68 }
69
70 BT_HIDDEN
71 void _bt_packet_set_is_frozen(const struct bt_packet *packet, bool is_frozen)
72 {
73 if (!packet) {
74 return;
75 }
76
77 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
78 "is-frozen=%d", packet, is_frozen);
79
80 if (packet->context_field) {
81 BT_LOGD_STR("Setting packet's context field's frozen state.");
82 bt_field_set_is_frozen(packet->context_field->field,
83 is_frozen);
84 }
85
86 ((struct bt_packet *) packet)->frozen = is_frozen;
87 }
88
89 static inline
90 void reset_counter_snapshots(struct bt_packet *packet)
91 {
92 packet->discarded_event_counter_snapshot.base.avail =
93 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
94 packet->packet_counter_snapshot.base.avail =
95 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
96 }
97
98 static inline
99 void reset_packet(struct bt_packet *packet)
100 {
101 BT_ASSERT(packet);
102 BT_LIB_LOGD("Resetting packet: %!+a", packet);
103 bt_packet_set_is_frozen(packet, false);
104
105 if (packet->context_field) {
106 bt_field_set_is_frozen(packet->context_field->field, false);
107 bt_field_reset(packet->context_field->field);
108 }
109
110 if (packet->default_beginning_cs) {
111 bt_clock_snapshot_reset(packet->default_beginning_cs);
112 }
113
114 if (packet->default_end_cs) {
115 bt_clock_snapshot_reset(packet->default_end_cs);
116 }
117
118 reset_counter_snapshots(packet);
119 }
120
121 static
122 void recycle_context_field(struct bt_field_wrapper *context_field,
123 struct bt_stream_class *stream_class)
124 {
125 BT_ASSERT(context_field);
126 BT_LIB_LOGD("Recycling packet context field: "
127 "addr=%p, %![sc-]+S, %![field-]+f", context_field,
128 stream_class, context_field->field);
129 bt_object_pool_recycle_object(&stream_class->packet_context_field_pool,
130 context_field);
131 }
132
133 BT_HIDDEN
134 void bt_packet_recycle(struct bt_packet *packet)
135 {
136 struct bt_stream *stream;
137
138 BT_ASSERT(packet);
139 BT_LIB_LOGD("Recycling packet: %!+a", packet);
140
141 /*
142 * Those are the important ordered steps:
143 *
144 * 1. Reset the packet object (put any permanent reference it
145 * has, unfreeze it and its fields in developer mode, etc.),
146 * but do NOT put its stream's reference. This stream
147 * contains the pool to which we're about to recycle this
148 * packet object, so we must guarantee its existence thanks
149 * to this existing reference.
150 *
151 * 2. Move the stream reference to our `stream`
152 * variable so that we can set the packet's stream member
153 * to NULL before recycling it. We CANNOT do this after
154 * we put the stream reference because this bt_object_put_ref()
155 * could destroy the stream, also destroying its
156 * packet pool, thus also destroying our packet object (this
157 * would result in an invalid write access).
158 *
159 * 3. Recycle the packet object.
160 *
161 * 4. Put our stream reference.
162 */
163 reset_packet(packet);
164 stream = packet->stream;
165 BT_ASSERT(stream);
166 packet->stream = NULL;
167 bt_object_pool_recycle_object(&stream->packet_pool, packet);
168 bt_object_put_no_null_check(&stream->base);
169 }
170
171 BT_HIDDEN
172 void bt_packet_destroy(struct bt_packet *packet)
173 {
174 BT_LIB_LOGD("Destroying packet: %!+a", packet);
175
176 if (packet->context_field) {
177 if (packet->stream) {
178 BT_LOGD_STR("Recycling packet's context field.");
179 recycle_context_field(packet->context_field,
180 packet->stream->class);
181 } else {
182 bt_field_wrapper_destroy(packet->context_field);
183 }
184
185 packet->context_field = NULL;
186 }
187
188 if (packet->default_beginning_cs) {
189 BT_LOGD_STR("Recycling beginning clock snapshot.");
190 bt_clock_snapshot_recycle(packet->default_beginning_cs);
191 packet->default_beginning_cs = NULL;
192 }
193
194 if (packet->default_end_cs) {
195 BT_LOGD_STR("Recycling end clock snapshot.");
196 bt_clock_snapshot_recycle(packet->default_end_cs);
197 packet->default_end_cs = NULL;
198 }
199
200 BT_LOGD_STR("Putting packet's stream.");
201 BT_OBJECT_PUT_REF_AND_RESET(packet->stream);
202 g_free(packet);
203 }
204
205 BT_HIDDEN
206 struct bt_packet *bt_packet_new(struct bt_stream *stream)
207 {
208 struct bt_packet *packet = NULL;
209 struct bt_trace_class *trace_class = NULL;
210
211 BT_ASSERT(stream);
212 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream);
213 packet = g_new0(struct bt_packet, 1);
214 if (!packet) {
215 BT_LOGE_STR("Failed to allocate one packet object.");
216 goto error;
217 }
218
219 bt_object_init_shared(&packet->base,
220 (bt_object_release_func) bt_packet_recycle);
221 packet->stream = stream;
222 bt_object_get_no_null_check(stream);
223 trace_class = bt_stream_class_borrow_trace_class_inline(stream->class);
224 BT_ASSERT(trace_class);
225
226 if (stream->class->packet_context_fc) {
227 BT_LOGD_STR("Creating initial packet context field.");
228 packet->context_field = bt_field_wrapper_create(
229 &stream->class->packet_context_field_pool,
230 stream->class->packet_context_fc);
231 if (!packet->context_field) {
232 BT_LOGE_STR("Cannot create packet context field wrapper.");
233 goto error;
234 }
235 }
236
237 if (stream->class->default_clock_class) {
238 if (stream->class->packets_have_default_beginning_cs) {
239 packet->default_beginning_cs = bt_clock_snapshot_create(
240 stream->class->default_clock_class);
241 if (!packet->default_beginning_cs) {
242 /* bt_clock_snapshot_create() logs errors */
243 goto error;
244 }
245 }
246
247 if (stream->class->packets_have_default_end_cs) {
248 packet->default_end_cs = bt_clock_snapshot_create(
249 stream->class->default_clock_class);
250 if (!packet->default_end_cs) {
251 /* bt_clock_snapshot_create() logs errors */
252 goto error;
253 }
254 }
255 }
256
257 reset_counter_snapshots(packet);
258 BT_LIB_LOGD("Created packet object: %!+a", packet);
259 goto end;
260
261 error:
262 BT_OBJECT_PUT_REF_AND_RESET(packet);
263
264 end:
265 return packet;
266 }
267
268 struct bt_packet *bt_packet_create(struct bt_stream *stream)
269 {
270 struct bt_packet *packet = NULL;
271
272 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
273 packet = bt_object_pool_create_object(&stream->packet_pool);
274 if (unlikely(!packet)) {
275 BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
276 "%![stream-]+s", stream);
277 goto end;
278 }
279
280 if (likely(!packet->stream)) {
281 packet->stream = stream;
282 bt_object_get_no_null_check_no_parent_check(
283 &packet->stream->base);
284 }
285
286 end:
287 return (void *) packet;
288 }
289
290 enum bt_packet_status bt_packet_move_context_field(struct bt_packet *packet,
291 struct bt_packet_context_field *context_field)
292 {
293 struct bt_stream_class *stream_class;
294 struct bt_field_wrapper *field_wrapper = (void *) context_field;
295
296 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
297 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Context field");
298 BT_ASSERT_PRE_HOT(packet, "Packet", ": %!+a", packet);
299 stream_class = packet->stream->class;
300 BT_ASSERT_PRE(stream_class->packet_context_fc,
301 "Stream class has no packet context field class: %!+S",
302 stream_class);
303 BT_ASSERT_PRE(field_wrapper->field->class ==
304 stream_class->packet_context_fc,
305 "Unexpected packet context field's class: "
306 "%![fc-]+F, %![expected-fc-]+F", field_wrapper->field->class,
307 stream_class->packet_context_fc);
308
309 /* Recycle current context field: always exists */
310 BT_ASSERT(packet->context_field);
311 recycle_context_field(packet->context_field, stream_class);
312
313 /* Move new field */
314 packet->context_field = field_wrapper;
315 return BT_PACKET_STATUS_OK;
316 }
317
318 void bt_packet_set_default_beginning_clock_snapshot(struct bt_packet *packet,
319 uint64_t value_cycles)
320 {
321 struct bt_stream_class *sc;
322
323 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
324 BT_ASSERT_PRE_PACKET_HOT(packet);
325 sc = packet->stream->class;
326 BT_ASSERT(sc);
327 BT_ASSERT_PRE(sc->default_clock_class,
328 "Packet's stream class has no default clock class: "
329 "%![packet-]+a, %![sc-]+S", packet, sc);
330 BT_ASSERT_PRE(sc->packets_have_default_beginning_cs,
331 "Packet's stream class indicates that its packets have "
332 "no default beginning clock snapshot: %![packet-]+a, %![sc-]+S",
333 packet, sc);
334 BT_ASSERT(packet->default_beginning_cs);
335 bt_clock_snapshot_set_value_inline(packet->default_beginning_cs,
336 value_cycles);
337 BT_LIB_LOGV("Set packet's default beginning clock snapshot: "
338 "%![packet-]+a, value=%" PRIu64, packet, value_cycles);
339 }
340
341 enum bt_clock_snapshot_state bt_packet_borrow_default_beginning_clock_snapshot(
342 const struct bt_packet *packet,
343 const struct bt_clock_snapshot **clock_snapshot)
344 {
345 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
346 BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot (output)");
347 *clock_snapshot = packet->default_beginning_cs;
348 return BT_CLOCK_SNAPSHOT_STATE_KNOWN;
349 }
350
351 void bt_packet_set_default_end_clock_snapshot(struct bt_packet *packet,
352 uint64_t value_cycles)
353 {
354 struct bt_stream_class *sc;
355
356 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
357 BT_ASSERT_PRE_PACKET_HOT(packet);
358 sc = packet->stream->class;
359 BT_ASSERT(sc);
360 BT_ASSERT_PRE(sc->default_clock_class,
361 "Packet's stream class has no default clock class: "
362 "%![packet-]+a, %![sc-]+S", packet, sc);
363 BT_ASSERT_PRE(sc->packets_have_default_end_cs,
364 "Packet's stream class indicates that its packets have "
365 "no default end clock snapshot: %![packet-]+a, %![sc-]+S",
366 packet, sc);
367 BT_ASSERT(packet->default_end_cs);
368 bt_clock_snapshot_set_value_inline(packet->default_end_cs, value_cycles);
369 BT_LIB_LOGV("Set packet's default end clock snapshot: "
370 "%![packet-]+a, value=%" PRIu64, packet, value_cycles);
371 }
372
373 enum bt_clock_snapshot_state bt_packet_borrow_default_end_clock_snapshot(
374 const struct bt_packet *packet,
375 const struct bt_clock_snapshot **clock_snapshot)
376 {
377 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
378 BT_ASSERT_PRE_NON_NULL(clock_snapshot, "Clock snapshot (output)");
379 *clock_snapshot = packet->default_end_cs;
380 return BT_CLOCK_SNAPSHOT_STATE_KNOWN;
381 }
382
383 enum bt_property_availability bt_packet_get_discarded_event_counter_snapshot(
384 const struct bt_packet *packet, uint64_t *value)
385 {
386 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
387 BT_ASSERT_PRE_NON_NULL(value, "Value (output)");
388 *value = packet->discarded_event_counter_snapshot.value;
389 return packet->discarded_event_counter_snapshot.base.avail;
390 }
391
392 void bt_packet_set_discarded_event_counter_snapshot(struct bt_packet *packet,
393 uint64_t value)
394 {
395 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
396 BT_ASSERT_PRE_PACKET_HOT(packet);
397 BT_ASSERT_PRE(packet->stream->class->packets_have_discarded_event_counter_snapshot,
398 "Packet's stream's discarded event counter is not enabled: "
399 "%![packet-]+a", packet);
400 bt_property_uint_set(&packet->discarded_event_counter_snapshot, value);
401 }
402
403 enum bt_property_availability bt_packet_get_packet_counter_snapshot(
404 const struct bt_packet *packet, uint64_t *value)
405 {
406 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
407 BT_ASSERT_PRE_NON_NULL(value, "Value (output)");
408 *value = packet->packet_counter_snapshot.value;
409 return packet->packet_counter_snapshot.base.avail;
410 }
411
412 void bt_packet_set_packet_counter_snapshot(struct bt_packet *packet,
413 uint64_t value)
414 {
415 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
416 BT_ASSERT_PRE_PACKET_HOT(packet);
417 BT_ASSERT_PRE(packet->stream->class->packets_have_packet_counter_snapshot,
418 "Packet's stream's packet counter is not enabled: "
419 "%![packet-]+a", packet);
420 bt_property_uint_set(&packet->packet_counter_snapshot, value);
421 }
422
423 void bt_packet_get_ref(const struct bt_packet *packet)
424 {
425 bt_object_get_ref(packet);
426 }
427
428 void bt_packet_put_ref(const struct bt_packet *packet)
429 {
430 bt_object_put_ref(packet);
431 }
This page took 0.039222 seconds and 4 git commands to generate.