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
);
108 void bt_packet_header_field_recycle(struct bt_field_wrapper
*header_field
,
109 struct bt_trace
*trace
)
111 BT_ASSERT(header_field
);
112 BT_LIB_LOGD("Recycling packet header field: "
113 "addr=%p, %![trace-]+t, %![field-]+f", header_field
,
114 trace
, header_field
->field
);
115 bt_object_pool_recycle_object(&trace
->packet_header_field_pool
,
120 void bt_packet_context_field_recycle(struct bt_field_wrapper
*context_field
,
121 struct bt_stream_class
*stream_class
)
123 BT_ASSERT(context_field
);
124 BT_LIB_LOGD("Recycling packet context field: "
125 "addr=%p, %![sc-]+S, %![field-]+f", context_field
,
126 stream_class
, context_field
->field
);
127 bt_object_pool_recycle_object(&stream_class
->packet_context_field_pool
,
132 void bt_packet_recycle(struct bt_packet
*packet
)
134 struct bt_stream
*stream
;
137 BT_LIB_LOGD("Recycling packet: %!+a", packet
);
140 * Those are the important ordered steps:
142 * 1. Reset the packet object (put any permanent reference it
143 * has, unfreeze it and its fields in developer mode, etc.),
144 * but do NOT put its stream's reference. This stream
145 * contains the pool to which we're about to recycle this
146 * packet object, so we must guarantee its existence thanks
147 * to this existing reference.
149 * 2. Move the stream reference to our `stream`
150 * variable so that we can set the packet's stream member
151 * to NULL before recycling it. We CANNOT do this after
152 * we put the stream reference because this bt_put()
153 * could destroy the stream, also destroying its
154 * packet pool, thus also destroying our packet object (this
155 * would result in an invalid write access).
157 * 3. Recycle the packet object.
159 * 4. Put our stream reference.
161 bt_packet_reset(packet
);
162 stream
= packet
->stream
;
164 packet
->stream
= NULL
;
165 bt_object_pool_recycle_object(&stream
->packet_pool
, packet
);
166 bt_object_put_no_null_check(&stream
->common
.base
);
170 void bt_packet_destroy(struct bt_packet
*packet
)
172 BT_LOGD("Destroying packet: addr=%p", packet
);
173 BT_LOGD_STR("Destroying packet's header field.");
175 if (packet
->header
) {
176 if (packet
->stream
) {
177 BT_LOGD_STR("Recycling packet's header field.");
178 bt_packet_header_field_recycle(packet
->header
,
179 bt_stream_class_borrow_trace(
180 bt_stream_borrow_class(packet
->stream
)));
182 bt_field_wrapper_destroy(packet
->header
);
186 if (packet
->context
) {
187 if (packet
->stream
) {
188 BT_LOGD_STR("Recycling packet's context field.");
189 bt_packet_context_field_recycle(packet
->context
,
190 bt_stream_borrow_class(packet
->stream
));
192 bt_field_wrapper_destroy(packet
->context
);
196 BT_LOGD_STR("Putting packet's stream.");
197 bt_put(packet
->stream
);
202 struct bt_packet
*bt_packet_new(struct bt_stream
*stream
)
204 struct bt_packet
*packet
= NULL
;
205 struct bt_stream_class
*stream_class
= NULL
;
206 struct bt_trace
*trace
= NULL
;
209 BT_LOGD("Creating packet object: stream-addr=%p, "
210 "stream-name=\"%s\", stream-class-addr=%p, "
211 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
212 stream
, bt_stream_get_name(stream
),
213 stream
->common
.stream_class
,
214 bt_stream_class_common_get_name(stream
->common
.stream_class
),
215 bt_stream_class_common_get_id(stream
->common
.stream_class
));
216 stream_class
= bt_stream_borrow_class(stream
);
217 BT_ASSERT(stream_class
);
218 trace
= bt_stream_class_borrow_trace(stream_class
);
220 packet
= g_new0(struct bt_packet
, 1);
222 BT_LOGE_STR("Failed to allocate one packet object.");
226 bt_object_init_shared(&packet
->base
,
227 (bt_object_release_func
) bt_packet_recycle
);
228 packet
->stream
= bt_get(stream
);
230 if (trace
->common
.packet_header_field_type
) {
231 BT_LOGD("Creating initial packet header field: ft-addr=%p",
232 trace
->common
.packet_header_field_type
);
233 packet
->header
= bt_field_wrapper_create(
234 &trace
->packet_header_field_pool
,
235 (void *) trace
->common
.packet_header_field_type
);
236 if (!packet
->header
) {
237 BT_LOGE("Cannot create packet header field wrapper.");
243 if (stream
->common
.stream_class
->packet_context_field_type
) {
244 BT_LOGD("Creating initial packet context field: ft-addr=%p",
245 stream
->common
.stream_class
->packet_context_field_type
);
246 packet
->context
= bt_field_wrapper_create(
247 &stream_class
->packet_context_field_pool
,
248 (void *) stream
->common
.stream_class
->packet_context_field_type
);
249 if (!packet
->context
) {
250 BT_LOGE("Cannot create packet context field wrapper.");
256 BT_LOGD("Created packet object: addr=%p", packet
);
262 struct bt_packet
*bt_packet_create(struct bt_stream
*stream
)
264 struct bt_packet
*packet
= NULL
;
266 BT_ASSERT_PRE_NON_NULL(stream
, "Stream");
267 packet
= bt_object_pool_create_object(&stream
->packet_pool
);
268 if (unlikely(!packet
)) {
269 BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
270 "%![stream-]+s", stream
);
274 if (unlikely(!packet
->stream
)) {
275 packet
->stream
= stream
;
276 bt_object_get_no_null_check_no_parent_check(
277 &packet
->stream
->common
.base
);
286 int bt_packet_move_header(struct bt_packet
*packet
,
287 struct bt_packet_header_field
*header_field
)
289 struct bt_trace
*trace
;
290 struct bt_field_wrapper
*field_wrapper
= (void *) header_field
;
292 BT_ASSERT_PRE_NON_NULL(packet
, "Event");
293 BT_ASSERT_PRE_NON_NULL(field_wrapper
, "Header field");
294 BT_ASSERT_PRE_HOT(packet
, "Packet", ": +%!+a", packet
);
295 trace
= bt_stream_class_borrow_trace(
296 bt_stream_borrow_class(packet
->stream
));
297 BT_ASSERT_PRE(trace
->common
.packet_header_field_type
,
298 "Trace has no packet header field type: %!+t",
301 /* TODO: compare field types (precondition) */
303 /* Recycle current header field: always exists */
304 BT_ASSERT(packet
->header
);
305 bt_packet_header_field_recycle(packet
->header
, trace
);
308 packet
->header
= field_wrapper
;
312 int bt_packet_move_context(struct bt_packet
*packet
,
313 struct bt_packet_context_field
*context_field
)
315 struct bt_stream_class
*stream_class
;
316 struct bt_field_wrapper
*field_wrapper
= (void *) context_field
;
318 BT_ASSERT_PRE_NON_NULL(packet
, "Event");
319 BT_ASSERT_PRE_NON_NULL(field_wrapper
, "Context field");
320 BT_ASSERT_PRE_HOT(packet
, "Packet", ": +%!+a", packet
);
321 stream_class
= bt_stream_borrow_class(packet
->stream
);
322 BT_ASSERT_PRE(stream_class
->common
.packet_context_field_type
,
323 "Stream class has no packet context field type: %!+S",
326 /* TODO: compare field types (precondition) */
328 /* Recycle current context field: always exists */
329 BT_ASSERT(packet
->context
);
330 bt_packet_context_field_recycle(packet
->context
, stream_class
);
333 packet
->context
= field_wrapper
;