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_freeze(struct bt_packet
*packet
)
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
, true);
78 if (packet
->context
) {
79 BT_LOGD_STR("Freezing packet's context field.");
80 bt_field_set_is_frozen_recursive((void *) packet
->context
->field
, true);
87 void bt_packet_reset(struct bt_packet
*packet
)
90 packet
->frozen
= false;
93 bt_field_set_is_frozen_recursive(
94 (void *) packet
->header
->field
, false);
95 bt_field_reset_recursive((void *) packet
->header
->field
);
98 if (packet
->context
) {
99 bt_field_set_is_frozen_recursive(
100 (void *) packet
->context
->field
, false);
101 bt_field_reset_recursive((void *) packet
->context
->field
);
106 void bt_packet_header_field_recycle(struct bt_field_wrapper
*header_field
,
107 struct bt_trace
*trace
)
109 BT_ASSERT(header_field
);
110 BT_LIB_LOGD("Recycling packet header field: "
111 "addr=%p, %![trace-]+t, %![field-]+f", header_field
,
112 trace
, header_field
->field
);
113 bt_object_pool_recycle_object(&trace
->packet_header_field_pool
,
118 void bt_packet_context_field_recycle(struct bt_field_wrapper
*context_field
,
119 struct bt_stream_class
*stream_class
)
121 BT_ASSERT(context_field
);
122 BT_LIB_LOGD("Recycling packet context field: "
123 "addr=%p, %![sc-]+S, %![field-]+f", context_field
,
124 stream_class
, context_field
->field
);
125 bt_object_pool_recycle_object(&stream_class
->packet_context_field_pool
,
130 void bt_packet_recycle(struct bt_packet
*packet
)
132 struct bt_stream
*stream
;
135 BT_LIB_LOGD("Recycling packet: %!+a", packet
);
138 * Those are the important ordered steps:
140 * 1. Reset the packet object (put any permanent reference it
141 * has, unfreeze it and its fields in developer mode, etc.),
142 * but do NOT put its stream's reference. This stream
143 * contains the pool to which we're about to recycle this
144 * packet object, so we must guarantee its existence thanks
145 * to this existing reference.
147 * 2. Move the stream reference to our `stream`
148 * variable so that we can set the packet's stream member
149 * to NULL before recycling it. We CANNOT do this after
150 * we put the stream reference because this bt_put()
151 * could destroy the stream, also destroying its
152 * packet pool, thus also destroying our packet object (this
153 * would result in an invalid write access).
155 * 3. Recycle the packet object.
157 * 4. Put our stream reference.
159 bt_packet_reset(packet
);
160 stream
= packet
->stream
;
162 packet
->stream
= NULL
;
163 bt_object_pool_recycle_object(&stream
->packet_pool
, packet
);
168 void bt_packet_destroy(struct bt_packet
*packet
)
170 BT_LOGD("Destroying packet: addr=%p", packet
);
171 BT_LOGD_STR("Destroying packet's header field.");
173 if (packet
->header
) {
174 if (packet
->stream
) {
175 BT_LOGD_STR("Recycling packet's header field.");
176 bt_packet_header_field_recycle(packet
->header
,
177 bt_stream_class_borrow_trace(
178 bt_stream_borrow_class(packet
->stream
)));
180 bt_field_wrapper_destroy(packet
->header
);
184 if (packet
->context
) {
185 if (packet
->stream
) {
186 BT_LOGD_STR("Recycling packet's context field.");
187 bt_packet_context_field_recycle(packet
->context
,
188 bt_stream_borrow_class(packet
->stream
));
190 bt_field_wrapper_destroy(packet
->context
);
194 BT_LOGD_STR("Putting packet's stream.");
195 bt_put(packet
->stream
);
200 struct bt_packet
*bt_packet_new(struct bt_stream
*stream
)
202 struct bt_packet
*packet
= NULL
;
203 struct bt_stream_class
*stream_class
= NULL
;
204 struct bt_trace
*trace
= NULL
;
207 BT_LOGD("Creating packet object: stream-addr=%p, "
208 "stream-name=\"%s\", stream-class-addr=%p, "
209 "stream-class-name=\"%s\", stream-class-id=%" PRId64
,
210 stream
, bt_stream_get_name(stream
),
211 stream
->common
.stream_class
,
212 bt_stream_class_common_get_name(stream
->common
.stream_class
),
213 bt_stream_class_common_get_id(stream
->common
.stream_class
));
214 stream_class
= bt_stream_borrow_class(stream
);
215 BT_ASSERT(stream_class
);
216 trace
= bt_stream_class_borrow_trace(stream_class
);
218 packet
= g_new0(struct bt_packet
, 1);
220 BT_LOGE_STR("Failed to allocate one packet object.");
224 bt_object_init_shared(&packet
->base
,
225 (bt_object_release_func
) bt_packet_recycle
);
226 packet
->stream
= bt_get(stream
);
228 if (trace
->common
.packet_header_field_type
) {
229 BT_LOGD("Creating initial packet header field: ft-addr=%p",
230 trace
->common
.packet_header_field_type
);
231 packet
->header
= bt_field_wrapper_create(
232 &trace
->packet_header_field_pool
,
233 (void *) trace
->common
.packet_header_field_type
);
234 if (!packet
->header
) {
235 BT_LOGE("Cannot create packet header field wrapper.");
241 if (stream
->common
.stream_class
->packet_context_field_type
) {
242 BT_LOGD("Creating initial packet context field: ft-addr=%p",
243 stream
->common
.stream_class
->packet_context_field_type
);
244 packet
->context
= bt_field_wrapper_create(
245 &stream_class
->packet_context_field_pool
,
246 (void *) stream
->common
.stream_class
->packet_context_field_type
);
247 if (!packet
->context
) {
248 BT_LOGE("Cannot create packet context field wrapper.");
254 BT_LOGD("Created packet object: addr=%p", packet
);
260 struct bt_packet
*bt_packet_create(struct bt_stream
*stream
)
262 struct bt_packet
*packet
= NULL
;
264 BT_ASSERT_PRE_NON_NULL(stream
, "Stream");
265 packet
= bt_object_pool_create_object(&stream
->packet_pool
);
267 BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
268 "%![stream-]+s", stream
);
272 if (!packet
->stream
) {
273 packet
->stream
= bt_get(stream
);
280 bt_packet_recycle(packet
);
288 int bt_packet_move_header(struct bt_packet
*packet
,
289 struct bt_packet_header_field
*header_field
)
291 struct bt_trace
*trace
;
292 struct bt_field_wrapper
*field_wrapper
= (void *) header_field
;
294 BT_ASSERT_PRE_NON_NULL(packet
, "Event");
295 BT_ASSERT_PRE_NON_NULL(field_wrapper
, "Header field");
296 BT_ASSERT_PRE_HOT(packet
, "Packet", ": +%!+a", packet
);
297 trace
= bt_stream_class_borrow_trace(
298 bt_stream_borrow_class(packet
->stream
));
299 BT_ASSERT_PRE(trace
->common
.packet_header_field_type
,
300 "Trace has no packet header field type: %!+t",
303 /* TODO: compare field types (precondition) */
305 /* Recycle current header field: always exists */
306 BT_ASSERT(packet
->header
);
307 bt_packet_header_field_recycle(packet
->header
, trace
);
310 packet
->header
= field_wrapper
;
314 int bt_packet_move_context(struct bt_packet
*packet
,
315 struct bt_packet_context_field
*context_field
)
317 struct bt_stream_class
*stream_class
;
318 struct bt_field_wrapper
*field_wrapper
= (void *) context_field
;
320 BT_ASSERT_PRE_NON_NULL(packet
, "Event");
321 BT_ASSERT_PRE_NON_NULL(field_wrapper
, "Context field");
322 BT_ASSERT_PRE_HOT(packet
, "Packet", ": +%!+a", packet
);
323 stream_class
= bt_stream_borrow_class(packet
->stream
);
324 BT_ASSERT_PRE(stream_class
->common
.packet_context_field_type
,
325 "Stream class has no packet context field type: %!+S",
328 /* TODO: compare field types (precondition) */
330 /* Recycle current context field: always exists */
331 BT_ASSERT(packet
->context
);
332 bt_packet_context_field_recycle(packet
->context
, stream_class
);
335 packet
->context
= field_wrapper
;