2 * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 #define BT_LOG_TAG "PACKET"
24 #include <babeltrace/lib-logging-internal.h>
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>
43 #define BT_ASSERT_PRE_PACKET_HOT(_packet) \
44 BT_ASSERT_PRE_HOT((_packet), "Packet", ": %!+a", (_packet))
46 struct bt_stream
*bt_packet_borrow_stream(struct bt_packet
*packet
)
48 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
49 return packet
->stream
;
52 const struct bt_stream
*bt_packet_borrow_stream_const(
53 const struct bt_packet
*packet
)
55 return bt_packet_borrow_stream((void *) packet
);
58 struct bt_field
*bt_packet_borrow_context_field(struct bt_packet
*packet
)
60 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
61 return packet
->context_field
? packet
->context_field
->field
: NULL
;
64 const struct bt_field
*bt_packet_borrow_context_field_const(
65 const struct bt_packet
*packet
)
67 return bt_packet_borrow_context_field((void *) packet
);
71 void _bt_packet_set_is_frozen(const struct bt_packet
*packet
, bool is_frozen
)
77 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
78 "is-frozen=%d", packet
, is_frozen
);
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
,
86 ((struct bt_packet
*) packet
)->frozen
= is_frozen
;
90 void reset_counter_snapshots(struct bt_packet
*packet
)
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
;
99 void reset_packet(struct bt_packet
*packet
)
102 BT_LIB_LOGD("Resetting packet: %!+a", packet
);
103 bt_packet_set_is_frozen(packet
, false);
105 if (packet
->context_field
) {
106 bt_field_set_is_frozen(packet
->context_field
->field
, false);
107 bt_field_reset(packet
->context_field
->field
);
110 if (packet
->default_beginning_cs
) {
111 bt_clock_snapshot_reset(packet
->default_beginning_cs
);
114 if (packet
->default_end_cs
) {
115 bt_clock_snapshot_reset(packet
->default_end_cs
);
118 reset_counter_snapshots(packet
);
122 void recycle_context_field(struct bt_field_wrapper
*context_field
,
123 struct bt_stream_class
*stream_class
)
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
,
134 void bt_packet_recycle(struct bt_packet
*packet
)
136 struct bt_stream
*stream
;
139 BT_LIB_LOGD("Recycling packet: %!+a", packet
);
142 * Those are the important ordered steps:
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.
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).
159 * 3. Recycle the packet object.
161 * 4. Put our stream reference.
163 reset_packet(packet
);
164 stream
= packet
->stream
;
166 packet
->stream
= NULL
;
167 bt_object_pool_recycle_object(&stream
->packet_pool
, packet
);
168 bt_object_put_no_null_check(&stream
->base
);
172 void bt_packet_destroy(struct bt_packet
*packet
)
174 BT_LIB_LOGD("Destroying packet: %!+a", packet
);
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);
182 bt_field_wrapper_destroy(packet
->context_field
);
185 packet
->context_field
= NULL
;
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
;
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
;
200 BT_LOGD_STR("Putting packet's stream.");
201 BT_OBJECT_PUT_REF_AND_RESET(packet
->stream
);
206 struct bt_packet
*bt_packet_new(struct bt_stream
*stream
)
208 struct bt_packet
*packet
= NULL
;
209 struct bt_trace_class
*trace_class
= NULL
;
212 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream
);
213 packet
= g_new0(struct bt_packet
, 1);
215 BT_LOGE_STR("Failed to allocate one packet object.");
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
);
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.");
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 */
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 */
257 reset_counter_snapshots(packet
);
258 BT_LIB_LOGD("Created packet object: %!+a", packet
);
262 BT_OBJECT_PUT_REF_AND_RESET(packet
);
268 struct bt_packet
*bt_packet_create(const struct bt_stream
*c_stream
)
270 struct bt_packet
*packet
= NULL
;
271 struct bt_stream
*stream
= (void *) c_stream
;
273 BT_ASSERT_PRE_NON_NULL(stream
, "Stream");
274 packet
= bt_object_pool_create_object(&stream
->packet_pool
);
275 if (unlikely(!packet
)) {
276 BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
277 "%![stream-]+s", stream
);
281 if (likely(!packet
->stream
)) {
282 packet
->stream
= stream
;
283 bt_object_get_no_null_check_no_parent_check(
284 &packet
->stream
->base
);
288 return (void *) packet
;
291 enum bt_packet_status
bt_packet_move_context_field(struct bt_packet
*packet
,
292 struct bt_packet_context_field
*context_field
)
294 struct bt_stream_class
*stream_class
;
295 struct bt_field_wrapper
*field_wrapper
= (void *) context_field
;
297 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
298 BT_ASSERT_PRE_NON_NULL(field_wrapper
, "Context field");
299 BT_ASSERT_PRE_HOT(packet
, "Packet", ": %!+a", packet
);
300 stream_class
= packet
->stream
->class;
301 BT_ASSERT_PRE(stream_class
->packet_context_fc
,
302 "Stream class has no packet context field class: %!+S",
304 BT_ASSERT_PRE(field_wrapper
->field
->class ==
305 stream_class
->packet_context_fc
,
306 "Unexpected packet context field's class: "
307 "%![fc-]+F, %![expected-fc-]+F", field_wrapper
->field
->class,
308 stream_class
->packet_context_fc
);
310 /* Recycle current context field: always exists */
311 BT_ASSERT(packet
->context_field
);
312 recycle_context_field(packet
->context_field
, stream_class
);
315 packet
->context_field
= field_wrapper
;
316 return BT_PACKET_STATUS_OK
;
319 void bt_packet_set_default_beginning_clock_snapshot(struct bt_packet
*packet
,
320 uint64_t value_cycles
)
322 struct bt_stream_class
*sc
;
324 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
325 BT_ASSERT_PRE_PACKET_HOT(packet
);
326 sc
= packet
->stream
->class;
328 BT_ASSERT_PRE(sc
->default_clock_class
,
329 "Packet's stream class has no default clock class: "
330 "%![packet-]+a, %![sc-]+S", packet
, sc
);
331 BT_ASSERT_PRE(sc
->packets_have_default_beginning_cs
,
332 "Packet's stream class indicates that its packets have "
333 "no default beginning clock snapshot: %![packet-]+a, %![sc-]+S",
335 BT_ASSERT(packet
->default_beginning_cs
);
336 bt_clock_snapshot_set_raw_value(packet
->default_beginning_cs
,
338 BT_LIB_LOGV("Set packet's default beginning clock snapshot: "
339 "%![packet-]+a, value=%" PRIu64
, packet
, value_cycles
);
342 enum bt_clock_snapshot_state
bt_packet_borrow_default_beginning_clock_snapshot(
343 const struct bt_packet
*packet
,
344 const struct bt_clock_snapshot
**clock_snapshot
)
346 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
347 BT_ASSERT_PRE_NON_NULL(clock_snapshot
, "Clock snapshot (output)");
348 *clock_snapshot
= packet
->default_beginning_cs
;
349 return BT_CLOCK_SNAPSHOT_STATE_KNOWN
;
352 void bt_packet_set_default_end_clock_snapshot(struct bt_packet
*packet
,
353 uint64_t value_cycles
)
355 struct bt_stream_class
*sc
;
357 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
358 BT_ASSERT_PRE_PACKET_HOT(packet
);
359 sc
= packet
->stream
->class;
361 BT_ASSERT_PRE(sc
->default_clock_class
,
362 "Packet's stream class has no default clock class: "
363 "%![packet-]+a, %![sc-]+S", packet
, sc
);
364 BT_ASSERT_PRE(sc
->packets_have_default_end_cs
,
365 "Packet's stream class indicates that its packets have "
366 "no default end clock snapshot: %![packet-]+a, %![sc-]+S",
368 BT_ASSERT(packet
->default_end_cs
);
369 bt_clock_snapshot_set_raw_value(packet
->default_end_cs
, value_cycles
);
370 BT_LIB_LOGV("Set packet's default end clock snapshot: "
371 "%![packet-]+a, value=%" PRIu64
, packet
, value_cycles
);
374 enum bt_clock_snapshot_state
bt_packet_borrow_default_end_clock_snapshot(
375 const struct bt_packet
*packet
,
376 const struct bt_clock_snapshot
**clock_snapshot
)
378 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
379 BT_ASSERT_PRE_NON_NULL(clock_snapshot
, "Clock snapshot (output)");
380 *clock_snapshot
= packet
->default_end_cs
;
381 return BT_CLOCK_SNAPSHOT_STATE_KNOWN
;
384 enum bt_property_availability
bt_packet_get_discarded_event_counter_snapshot(
385 const struct bt_packet
*packet
, uint64_t *value
)
387 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
388 BT_ASSERT_PRE_NON_NULL(value
, "Value (output)");
389 *value
= packet
->discarded_event_counter_snapshot
.value
;
390 return packet
->discarded_event_counter_snapshot
.base
.avail
;
393 void bt_packet_set_discarded_event_counter_snapshot(struct bt_packet
*packet
,
396 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
397 BT_ASSERT_PRE_PACKET_HOT(packet
);
398 BT_ASSERT_PRE(packet
->stream
->class->packets_have_discarded_event_counter_snapshot
,
399 "Packet's stream's discarded event counter is not enabled: "
400 "%![packet-]+a", packet
);
401 bt_property_uint_set(&packet
->discarded_event_counter_snapshot
, value
);
404 enum bt_property_availability
bt_packet_get_packet_counter_snapshot(
405 const struct bt_packet
*packet
, uint64_t *value
)
407 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
408 BT_ASSERT_PRE_NON_NULL(value
, "Value (output)");
409 *value
= packet
->packet_counter_snapshot
.value
;
410 return packet
->packet_counter_snapshot
.base
.avail
;
413 void bt_packet_set_packet_counter_snapshot(struct bt_packet
*packet
,
416 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
417 BT_ASSERT_PRE_PACKET_HOT(packet
);
418 BT_ASSERT_PRE(packet
->stream
->class->packets_have_packet_counter_snapshot
,
419 "Packet's stream's packet counter is not enabled: "
420 "%![packet-]+a", packet
);
421 bt_property_uint_set(&packet
->packet_counter_snapshot
, value
);
424 void bt_packet_get_ref(const struct bt_packet
*packet
)
426 bt_object_get_ref(packet
);
429 void bt_packet_put_ref(const struct bt_packet
*packet
)
431 bt_object_put_ref(packet
);