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
)
71 BT_LOGD("Setting packet's frozen state: addr=%p, frozen=%d",
75 BT_LOGD("Setting packet's header field's frozen state: "
76 "frozen=%d", is_frozen
);
77 bt_field_set_is_frozen_recursive((void *) packet
->header
->field
,
81 if (packet
->context
) {
82 BT_LOGD("Setting packet's context field's frozen state: "
83 "frozen=%d", is_frozen
);
84 bt_field_set_is_frozen_recursive((void *) packet
->context
->field
,
88 packet
->frozen
= is_frozen
;
92 void bt_packet_reset(struct bt_packet
*packet
)
95 bt_packet_set_is_frozen(packet
, false);
98 bt_field_set_is_frozen_recursive(
99 (void *) packet
->header
->field
, false);
100 bt_field_reset_recursive((void *) packet
->header
->field
);
103 if (packet
->context
) {
104 bt_field_set_is_frozen_recursive(
105 (void *) packet
->context
->field
, false);
106 bt_field_reset_recursive((void *) packet
->context
->field
);
109 bt_clock_value_set_reset(&packet
->begin_cv_set
);
110 bt_clock_value_set_reset(&packet
->end_cv_set
);
114 void bt_packet_header_field_recycle(struct bt_field_wrapper
*header_field
,
115 struct bt_trace
*trace
)
117 BT_ASSERT(header_field
);
118 BT_LIB_LOGD("Recycling packet header field: "
119 "addr=%p, %![trace-]+t, %![field-]+f", header_field
,
120 trace
, header_field
->field
);
121 bt_object_pool_recycle_object(&trace
->packet_header_field_pool
,
126 void bt_packet_context_field_recycle(struct bt_field_wrapper
*context_field
,
127 struct bt_stream_class
*stream_class
)
129 BT_ASSERT(context_field
);
130 BT_LIB_LOGD("Recycling packet context field: "
131 "addr=%p, %![sc-]+S, %![field-]+f", context_field
,
132 stream_class
, context_field
->field
);
133 bt_object_pool_recycle_object(&stream_class
->packet_context_field_pool
,
138 void bt_packet_recycle(struct bt_packet
*packet
)
140 struct bt_stream
*stream
;
143 BT_LIB_LOGD("Recycling packet: %!+a", packet
);
146 * Those are the important ordered steps:
148 * 1. Reset the packet object (put any permanent reference it
149 * has, unfreeze it and its fields in developer mode, etc.),
150 * but do NOT put its stream's reference. This stream
151 * contains the pool to which we're about to recycle this
152 * packet object, so we must guarantee its existence thanks
153 * to this existing reference.
155 * 2. Move the stream reference to our `stream`
156 * variable so that we can set the packet's stream member
157 * to NULL before recycling it. We CANNOT do this after
158 * we put the stream reference because this bt_put()
159 * could destroy the stream, also destroying its
160 * packet pool, thus also destroying our packet object (this
161 * would result in an invalid write access).
163 * 3. Recycle the packet object.
165 * 4. Put our stream reference.
167 bt_packet_reset(packet
);
168 stream
= packet
->stream
;
170 packet
->stream
= NULL
;
171 bt_object_pool_recycle_object(&stream
->packet_pool
, packet
);
172 bt_object_put_no_null_check(&stream
->common
.base
);
176 void bt_packet_destroy(struct bt_packet
*packet
)
178 BT_LOGD("Destroying packet: addr=%p", packet
);
179 BT_LOGD_STR("Destroying packet's header field.");
181 if (packet
->header
) {
182 if (packet
->stream
) {
183 BT_LOGD_STR("Recycling packet's header field.");
184 bt_packet_header_field_recycle(packet
->header
,
185 bt_stream_class_borrow_trace(
186 bt_stream_borrow_class(packet
->stream
)));
188 bt_field_wrapper_destroy(packet
->header
);
192 if (packet
->context
) {
193 if (packet
->stream
) {
194 BT_LOGD_STR("Recycling packet's context field.");
195 bt_packet_context_field_recycle(packet
->context
,
196 bt_stream_borrow_class(packet
->stream
));
198 bt_field_wrapper_destroy(packet
->context
);
202 bt_clock_value_set_finalize(&packet
->begin_cv_set
);
203 bt_clock_value_set_finalize(&packet
->end_cv_set
);
204 BT_LOGD_STR("Putting packet's stream.");
205 bt_put(packet
->stream
);
210 struct bt_packet
*bt_packet_new(struct bt_stream
*stream
)
212 struct bt_packet
*packet
= NULL
;
213 struct bt_stream_class
*stream_class
= NULL
;
214 struct bt_trace
*trace
= NULL
;
217 BT_LOGD("Creating packet object: stream-addr=%p, "
218 "stream-name=\"%s\", stream-class-addr=%p, "
219 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
220 stream
, bt_stream_get_name(stream
),
221 stream
->common
.stream_class
,
222 bt_stream_class_common_get_name(stream
->common
.stream_class
),
223 bt_stream_class_common_get_id(stream
->common
.stream_class
));
224 stream_class
= bt_stream_borrow_class(stream
);
225 BT_ASSERT(stream_class
);
226 trace
= bt_stream_class_borrow_trace(stream_class
);
228 packet
= g_new0(struct bt_packet
, 1);
230 BT_LOGE_STR("Failed to allocate one packet object.");
234 bt_object_init_shared(&packet
->base
,
235 (bt_object_release_func
) bt_packet_recycle
);
236 packet
->stream
= bt_get(stream
);
238 if (trace
->common
.packet_header_field_type
) {
239 BT_LOGD("Creating initial packet header field: ft-addr=%p",
240 trace
->common
.packet_header_field_type
);
241 packet
->header
= bt_field_wrapper_create(
242 &trace
->packet_header_field_pool
,
243 (void *) trace
->common
.packet_header_field_type
);
244 if (!packet
->header
) {
245 BT_LOGE("Cannot create packet header field wrapper.");
251 if (stream
->common
.stream_class
->packet_context_field_type
) {
252 BT_LOGD("Creating initial packet context field: ft-addr=%p",
253 stream
->common
.stream_class
->packet_context_field_type
);
254 packet
->context
= bt_field_wrapper_create(
255 &stream_class
->packet_context_field_pool
,
256 (void *) stream
->common
.stream_class
->packet_context_field_type
);
257 if (!packet
->context
) {
258 BT_LOGE("Cannot create packet context field wrapper.");
264 if (bt_clock_value_set_initialize(&packet
->begin_cv_set
)) {
269 if (bt_clock_value_set_initialize(&packet
->end_cv_set
)) {
274 BT_LOGD("Created packet object: addr=%p", packet
);
280 struct bt_packet
*bt_packet_create(struct bt_stream
*stream
)
282 struct bt_packet
*packet
= NULL
;
284 BT_ASSERT_PRE_NON_NULL(stream
, "Stream");
285 packet
= bt_object_pool_create_object(&stream
->packet_pool
);
286 if (unlikely(!packet
)) {
287 BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
288 "%![stream-]+s", stream
);
292 if (unlikely(!packet
->stream
)) {
293 packet
->stream
= stream
;
294 bt_object_get_no_null_check_no_parent_check(
295 &packet
->stream
->common
.base
);
304 int bt_packet_move_header(struct bt_packet
*packet
,
305 struct bt_packet_header_field
*header_field
)
307 struct bt_trace
*trace
;
308 struct bt_field_wrapper
*field_wrapper
= (void *) header_field
;
310 BT_ASSERT_PRE_NON_NULL(packet
, "Event");
311 BT_ASSERT_PRE_NON_NULL(field_wrapper
, "Header field");
312 BT_ASSERT_PRE_HOT(packet
, "Packet", ": %!+a", packet
);
313 trace
= bt_stream_class_borrow_trace(
314 bt_stream_borrow_class(packet
->stream
));
315 BT_ASSERT_PRE(trace
->common
.packet_header_field_type
,
316 "Trace has no packet header field type: %!+t",
319 /* TODO: compare field types (precondition) */
321 /* Recycle current header field: always exists */
322 BT_ASSERT(packet
->header
);
323 bt_packet_header_field_recycle(packet
->header
, trace
);
326 packet
->header
= field_wrapper
;
330 int bt_packet_move_context(struct bt_packet
*packet
,
331 struct bt_packet_context_field
*context_field
)
333 struct bt_stream_class
*stream_class
;
334 struct bt_field_wrapper
*field_wrapper
= (void *) context_field
;
336 BT_ASSERT_PRE_NON_NULL(packet
, "Event");
337 BT_ASSERT_PRE_NON_NULL(field_wrapper
, "Context field");
338 BT_ASSERT_PRE_HOT(packet
, "Packet", ": %!+a", packet
);
339 stream_class
= bt_stream_borrow_class(packet
->stream
);
340 BT_ASSERT_PRE(stream_class
->common
.packet_context_field_type
,
341 "Stream class has no packet context field type: %!+S",
344 /* TODO: compare field types (precondition) */
346 /* Recycle current context field: always exists */
347 BT_ASSERT(packet
->context
);
348 bt_packet_context_field_recycle(packet
->context
, stream_class
);
351 packet
->context
= field_wrapper
;
355 int bt_packet_set_beginning_clock_value(struct bt_packet
*packet
,
356 struct bt_clock_class
*clock_class
, uint64_t raw_value
,
359 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
360 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
361 BT_ASSERT_PRE_HOT(packet
, "Packet", ": %!+a", packet
);
362 BT_ASSERT_PRE(is_default
,
363 "You can only set a default clock value as of this version.");
364 return bt_clock_value_set_set_clock_value(&packet
->begin_cv_set
,
365 clock_class
, raw_value
, is_default
);
368 struct bt_clock_value
*bt_packet_borrow_default_begin_clock_value(
369 struct bt_packet
*packet
)
371 struct bt_clock_value
*clock_value
= NULL
;
373 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
374 clock_value
= packet
->begin_cv_set
.default_cv
;
376 BT_LIB_LOGV("No default clock value: %![packet-]+a", packet
);
382 int bt_packet_set_end_clock_value(struct bt_packet
*packet
,
383 struct bt_clock_class
*clock_class
, uint64_t raw_value
,
386 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
387 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
388 BT_ASSERT_PRE_HOT(packet
, "Packet", ": %!+a", packet
);
389 BT_ASSERT_PRE(is_default
,
390 "You can only set a default clock value as of this version.");
391 return bt_clock_value_set_set_clock_value(&packet
->end_cv_set
,
392 clock_class
, raw_value
, is_default
);
395 struct bt_clock_value
*bt_packet_borrow_default_end_clock_value(
396 struct bt_packet
*packet
)
398 struct bt_clock_value
*clock_value
= NULL
;
400 BT_ASSERT_PRE_NON_NULL(packet
, "Packet");
401 clock_value
= packet
->end_cv_set
.default_cv
;
403 BT_LIB_LOGV("No default clock value: %![packet-]+a", packet
);