4 * Babeltrace CTF IR - Stream packet
6 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #define BT_LOG_TAG "PACKET"
28 #include <babeltrace/lib-logging-internal.h>
30 #include <babeltrace/assert-pre-internal.h>
31 #include <babeltrace/ctf-ir/fields-internal.h>
32 #include <babeltrace/ctf-ir/packet.h>
33 #include <babeltrace/ctf-ir/packet-internal.h>
34 #include <babeltrace/ctf-ir/field-wrapper-internal.h>
35 #include <babeltrace/ctf-ir/trace.h>
36 #include <babeltrace/ctf-ir/stream-class-internal.h>
37 #include <babeltrace/ctf-ir/stream-class.h>
38 #include <babeltrace/ctf-ir/stream.h>
39 #include <babeltrace/ctf-ir/stream-internal.h>
40 #include <babeltrace/ctf-ir/trace-internal.h>
41 #include <babeltrace/object-internal.h>
42 #include <babeltrace/ref.h>
43 #include <babeltrace/assert-internal.h>
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 struct bt_field
*bt_packet_borrow_header(struct bt_packet
*packet
)
54 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
55 return packet
->header
? (void *) packet
->header
->field
: NULL
;
58 struct bt_field
*bt_packet_borrow_context(struct bt_packet
*packet
)
60 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
61 return packet
->context
? (void *) packet
->context
->field
: NULL
;
65 void _bt_packet_set_is_frozen(struct bt_packet
*packet
, bool is_frozen
)
67 if (!packet
|| packet
->frozen
) {
71 BT_LOGD("Freezing packet: addr=%p", packet
);
74 BT_LOGD_STR("Freezing packet's header field.");
75 bt_field_set_is_frozen_recursive((void *) packet
->header
->field
,
79 if (packet
->context
) {
80 BT_LOGD_STR("Freezing packet's context field.");
81 bt_field_set_is_frozen_recursive((void *) packet
->context
->field
,
85 packet
->frozen
= is_frozen
;
89 void bt_packet_reset(struct bt_packet
*packet
)
92 bt_packet_set_is_frozen(packet
, false);
95 bt_field_set_is_frozen_recursive(
96 (void *) packet
->header
->field
, false);
97 bt_field_reset_recursive((void *) packet
->header
->field
);
100 if (packet
->context
) {
101 bt_field_set_is_frozen_recursive(
102 (void *) packet
->context
->field
, false);
103 bt_field_reset_recursive((void *) packet
->context
->field
);
106 bt_clock_value_set_reset(&packet
->begin_cv_set
);
107 bt_clock_value_set_reset(&packet
->end_cv_set
);
111 void bt_packet_header_field_recycle(struct bt_field_wrapper
*header_field
,
112 struct bt_trace
*trace
)
114 BT_ASSERT(header_field
);
115 BT_LIB_LOGD("Recycling packet header field: "
116 "addr=%p, %![trace-]+t, %![field-]+f", header_field
,
117 trace
, header_field
->field
);
118 bt_object_pool_recycle_object(&trace
->packet_header_field_pool
,
123 void bt_packet_context_field_recycle(struct bt_field_wrapper
*context_field
,
124 struct bt_stream_class
*stream_class
)
126 BT_ASSERT(context_field
);
127 BT_LIB_LOGD("Recycling packet context field: "
128 "addr=%p, %![sc-]+S, %![field-]+f", context_field
,
129 stream_class
, context_field
->field
);
130 bt_object_pool_recycle_object(&stream_class
->packet_context_field_pool
,
135 void bt_packet_recycle(struct bt_packet
*packet
)
137 struct bt_stream
*stream
;
140 BT_LIB_LOGD("Recycling packet: %!+a", packet
);
143 * Those are the important ordered steps:
145 * 1. Reset the packet object (put any permanent reference it
146 * has, unfreeze it and its fields in developer mode, etc.),
147 * but do NOT put its stream's reference. This stream
148 * contains the pool to which we're about to recycle this
149 * packet object, so we must guarantee its existence thanks
150 * to this existing reference.
152 * 2. Move the stream reference to our `stream`
153 * variable so that we can set the packet's stream member
154 * to NULL before recycling it. We CANNOT do this after
155 * we put the stream reference because this bt_put()
156 * could destroy the stream, also destroying its
157 * packet pool, thus also destroying our packet object (this
158 * would result in an invalid write access).
160 * 3. Recycle the packet object.
162 * 4. Put our stream reference.
164 bt_packet_reset(packet
);
165 stream
= packet
->stream
;
167 packet
->stream
= NULL
;
168 bt_object_pool_recycle_object(&stream
->packet_pool
, packet
);
169 bt_object_put_no_null_check(&stream
->common
.base
);
173 void bt_packet_destroy(struct bt_packet
*packet
)
175 BT_LOGD("Destroying packet: addr=%p", packet
);
176 BT_LOGD_STR("Destroying packet's header field.");
178 if (packet
->header
) {
179 if (packet
->stream
) {
180 BT_LOGD_STR("Recycling packet's header field.");
181 bt_packet_header_field_recycle(packet
->header
,
182 bt_stream_class_borrow_trace(
183 bt_stream_borrow_class(packet
->stream
)));
185 bt_field_wrapper_destroy(packet
->header
);
189 if (packet
->context
) {
190 if (packet
->stream
) {
191 BT_LOGD_STR("Recycling packet's context field.");
192 bt_packet_context_field_recycle(packet
->context
,
193 bt_stream_borrow_class(packet
->stream
));
195 bt_field_wrapper_destroy(packet
->context
);
199 bt_clock_value_set_finalize(&packet
->begin_cv_set
);
200 bt_clock_value_set_finalize(&packet
->end_cv_set
);
201 BT_LOGD_STR("Putting packet's stream.");
202 bt_put(packet
->stream
);
207 struct bt_packet
*bt_packet_new(struct bt_stream
*stream
)
209 struct bt_packet
*packet
= NULL
;
210 struct bt_stream_class
*stream_class
= NULL
;
211 struct bt_trace
*trace
= NULL
;
214 BT_LOGD("Creating packet object: stream-addr=%p, "
215 "stream-name=\"%s\", stream-class-addr=%p, "
216 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
217 stream
, bt_stream_get_name(stream
),
218 stream
->common
.stream_class
,
219 bt_stream_class_common_get_name(stream
->common
.stream_class
),
220 bt_stream_class_common_get_id(stream
->common
.stream_class
));
221 stream_class
= bt_stream_borrow_class(stream
);
222 BT_ASSERT(stream_class
);
223 trace
= bt_stream_class_borrow_trace(stream_class
);
225 packet
= g_new0(struct bt_packet
, 1);
227 BT_LOGE_STR("Failed to allocate one packet object.");
231 bt_object_init_shared(&packet
->base
,
232 (bt_object_release_func
) bt_packet_recycle
);
233 packet
->stream
= bt_get(stream
);
235 if (trace
->common
.packet_header_field_type
) {
236 BT_LOGD("Creating initial packet header field: ft-addr=%p",
237 trace
->common
.packet_header_field_type
);
238 packet
->header
= bt_field_wrapper_create(
239 &trace
->packet_header_field_pool
,
240 (void *) trace
->common
.packet_header_field_type
);
241 if (!packet
->header
) {
242 BT_LOGE("Cannot create packet header field wrapper.");
248 if (stream
->common
.stream_class
->packet_context_field_type
) {
249 BT_LOGD("Creating initial packet context field: ft-addr=%p",
250 stream
->common
.stream_class
->packet_context_field_type
);
251 packet
->context
= bt_field_wrapper_create(
252 &stream_class
->packet_context_field_pool
,
253 (void *) stream
->common
.stream_class
->packet_context_field_type
);
254 if (!packet
->context
) {
255 BT_LOGE("Cannot create packet context field wrapper.");
261 if (bt_clock_value_set_initialize(&packet
->begin_cv_set
)) {
266 if (bt_clock_value_set_initialize(&packet
->end_cv_set
)) {
271 BT_LOGD("Created packet object: addr=%p", packet
);
277 struct bt_packet
*bt_packet_create(struct bt_stream
*stream
)
279 struct bt_packet
*packet
= NULL
;
281 BT_ASSERT_PRE_NON_NULL(stream
, "Stream");
282 packet
= bt_object_pool_create_object(&stream
->packet_pool
);
283 if (unlikely(!packet
)) {
284 BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
285 "%![stream-]+s", stream
);
289 if (unlikely(!packet
->stream
)) {
290 packet
->stream
= stream
;
291 bt_object_get_no_null_check_no_parent_check(
292 &packet
->stream
->common
.base
);
301 int bt_packet_move_header(struct bt_packet
*packet
,
302 struct bt_packet_header_field
*header_field
)
304 struct bt_trace
*trace
;
305 struct bt_field_wrapper
*field_wrapper
= (void *) header_field
;
307 BT_ASSERT_PRE_NON_NULL(packet
, "Event");
308 BT_ASSERT_PRE_NON_NULL(field_wrapper
, "Header field");
309 BT_ASSERT_PRE_HOT(packet
, "Packet", ": %!+a", packet
);
310 trace
= bt_stream_class_borrow_trace(
311 bt_stream_borrow_class(packet
->stream
));
312 BT_ASSERT_PRE(trace
->common
.packet_header_field_type
,
313 "Trace has no packet header field type: %!+t",
316 /* TODO: compare field types (precondition) */
318 /* Recycle current header field: always exists */
319 BT_ASSERT(packet
->header
);
320 bt_packet_header_field_recycle(packet
->header
, trace
);
323 packet
->header
= field_wrapper
;
327 int bt_packet_move_context(struct bt_packet
*packet
,
328 struct bt_packet_context_field
*context_field
)
330 struct bt_stream_class
*stream_class
;
331 struct bt_field_wrapper
*field_wrapper
= (void *) context_field
;
333 BT_ASSERT_PRE_NON_NULL(packet
, "Event");
334 BT_ASSERT_PRE_NON_NULL(field_wrapper
, "Context field");
335 BT_ASSERT_PRE_HOT(packet
, "Packet", ": %!+a", packet
);
336 stream_class
= bt_stream_borrow_class(packet
->stream
);
337 BT_ASSERT_PRE(stream_class
->common
.packet_context_field_type
,
338 "Stream class has no packet context field type: %!+S",
341 /* TODO: compare field types (precondition) */
343 /* Recycle current context field: always exists */
344 BT_ASSERT(packet
->context
);
345 bt_packet_context_field_recycle(packet
->context
, stream_class
);
348 packet
->context
= field_wrapper
;
352 int bt_packet_set_beginning_clock_value(struct bt_packet
*packet
,
353 struct bt_clock_class
*clock_class
, uint64_t raw_value
,
356 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
357 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
358 BT_ASSERT_PRE_HOT(packet
, "Packet", ": %!+a", packet
);
359 BT_ASSERT_PRE(is_default
,
360 "You can only set a default clock value as of this version.");
361 return bt_clock_value_set_set_clock_value(&packet
->begin_cv_set
,
362 clock_class
, raw_value
, is_default
);
365 struct bt_clock_value
*bt_packet_borrow_default_begin_clock_value(
366 struct bt_packet
*packet
)
368 struct bt_clock_value
*clock_value
= NULL
;
370 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
371 clock_value
= packet
->begin_cv_set
.default_cv
;
373 BT_LIB_LOGV("No default clock value: %![packet-]+a", packet
);
379 int bt_packet_set_end_clock_value(struct bt_packet
*packet
,
380 struct bt_clock_class
*clock_class
, uint64_t raw_value
,
383 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
384 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
385 BT_ASSERT_PRE_HOT(packet
, "Packet", ": %!+a", packet
);
386 BT_ASSERT_PRE(is_default
,
387 "You can only set a default clock value as of this version.");
388 return bt_clock_value_set_set_clock_value(&packet
->end_cv_set
,
389 clock_class
, raw_value
, is_default
);
392 struct bt_clock_value
*bt_packet_borrow_default_end_clock_value(
393 struct bt_packet
*packet
)
395 struct bt_clock_value
*clock_value
= NULL
;
397 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
398 clock_value
= packet
->end_cv_set
.default_cv
;
400 BT_LIB_LOGV("No default clock value: %![packet-]+a", packet
);