lib: make packets and packet messages optional, disabled by default
[babeltrace.git] / src / lib / trace-ir / packet.c
CommitLineData
f79cf0f0 1/*
e2f7325d 2 * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com>
f79cf0f0
PP
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
20 * SOFTWARE.
21 */
22
350ad6c1 23#define BT_LOG_TAG "LIB/PACKET"
c2d9d9cf 24#include "lib/logging.h"
8ea705f0 25
578e048b 26#include "lib/assert-pre.h"
3fadfbc0
MJ
27#include <babeltrace2/trace-ir/packet-const.h>
28#include <babeltrace2/trace-ir/packet.h>
3fadfbc0 29#include <babeltrace2/trace-ir/trace.h>
3fadfbc0
MJ
30#include <babeltrace2/trace-ir/stream-class.h>
31#include <babeltrace2/trace-ir/stream.h>
578e048b
MJ
32#include "lib/object.h"
33#include "common/assert.h"
be514b0c 34#include <inttypes.h>
f79cf0f0 35
578e048b
MJ
36#include "field.h"
37#include "field-wrapper.h"
38#include "packet.h"
39#include "stream-class.h"
40#include "stream.h"
41#include "trace.h"
d24d5663 42#include "lib/func-status.h"
578e048b 43
44c440bc
PP
44#define BT_ASSERT_PRE_PACKET_HOT(_packet) \
45 BT_ASSERT_PRE_HOT((_packet), "Packet", ": %!+a", (_packet))
46
094ff7c0 47struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet)
f79cf0f0 48{
f6ccaed9 49 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
094ff7c0 50 return packet->stream;
f79cf0f0
PP
51}
52
40f4ba76
PP
53const struct bt_stream *bt_packet_borrow_stream_const(
54 const struct bt_packet *packet)
e5be10ef 55{
40f4ba76 56 return bt_packet_borrow_stream((void *) packet);
e5be10ef
PP
57}
58
44c440bc 59struct bt_field *bt_packet_borrow_context_field(struct bt_packet *packet)
f79cf0f0 60{
312c056a 61 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
44c440bc 62 return packet->context_field ? packet->context_field->field : NULL;
312c056a 63}
c5c82c6e 64
40f4ba76
PP
65const struct bt_field *bt_packet_borrow_context_field_const(
66 const struct bt_packet *packet)
e5be10ef 67{
40f4ba76 68 return bt_packet_borrow_context_field((void *) packet);
e5be10ef
PP
69}
70
312c056a 71BT_HIDDEN
40f4ba76 72void _bt_packet_set_is_frozen(const struct bt_packet *packet, bool is_frozen)
312c056a 73{
59e3c4db 74 if (!packet) {
312c056a 75 return;
c5c82c6e
PP
76 }
77
44c440bc
PP
78 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
79 "is-frozen=%d", packet, is_frozen);
312c056a 80
44c440bc
PP
81 if (packet->context_field) {
82 BT_LOGD_STR("Setting packet's context field's frozen state.");
83 bt_field_set_is_frozen(packet->context_field->field,
6c677fb5 84 is_frozen);
312c056a
PP
85 }
86
40f4ba76 87 ((struct bt_packet *) packet)->frozen = is_frozen;
f6ccaed9
PP
88}
89
312c056a 90static inline
44c440bc 91void reset_packet(struct bt_packet *packet)
f6ccaed9 92{
312c056a 93 BT_ASSERT(packet);
44c440bc 94 BT_LIB_LOGD("Resetting packet: %!+a", packet);
6c677fb5 95 bt_packet_set_is_frozen(packet, false);
312c056a 96
44c440bc
PP
97 if (packet->context_field) {
98 bt_field_set_is_frozen(packet->context_field->field, false);
99 bt_field_reset(packet->context_field->field);
312c056a 100 }
f79cf0f0
PP
101}
102
312c056a 103static
44c440bc 104void recycle_context_field(struct bt_field_wrapper *context_field,
312c056a 105 struct bt_stream_class *stream_class)
f79cf0f0 106{
312c056a
PP
107 BT_ASSERT(context_field);
108 BT_LIB_LOGD("Recycling packet context field: "
109 "addr=%p, %![sc-]+S, %![field-]+f", context_field,
110 stream_class, context_field->field);
111 bt_object_pool_recycle_object(&stream_class->packet_context_field_pool,
112 context_field);
f79cf0f0
PP
113}
114
115BT_HIDDEN
312c056a 116void bt_packet_recycle(struct bt_packet *packet)
f79cf0f0 117{
312c056a 118 struct bt_stream *stream;
f79cf0f0 119
312c056a
PP
120 BT_ASSERT(packet);
121 BT_LIB_LOGD("Recycling packet: %!+a", packet);
122
123 /*
124 * Those are the important ordered steps:
125 *
126 * 1. Reset the packet object (put any permanent reference it
127 * has, unfreeze it and its fields in developer mode, etc.),
128 * but do NOT put its stream's reference. This stream
129 * contains the pool to which we're about to recycle this
130 * packet object, so we must guarantee its existence thanks
131 * to this existing reference.
132 *
133 * 2. Move the stream reference to our `stream`
134 * variable so that we can set the packet's stream member
135 * to NULL before recycling it. We CANNOT do this after
65300d60 136 * we put the stream reference because this bt_object_put_ref()
312c056a
PP
137 * could destroy the stream, also destroying its
138 * packet pool, thus also destroying our packet object (this
139 * would result in an invalid write access).
140 *
141 * 3. Recycle the packet object.
142 *
143 * 4. Put our stream reference.
144 */
44c440bc 145 reset_packet(packet);
312c056a
PP
146 stream = packet->stream;
147 BT_ASSERT(stream);
148 packet->stream = NULL;
149 bt_object_pool_recycle_object(&stream->packet_pool, packet);
cb6f1f7d 150 bt_object_put_no_null_check(&stream->base);
f79cf0f0
PP
151}
152
312c056a
PP
153BT_HIDDEN
154void bt_packet_destroy(struct bt_packet *packet)
f79cf0f0 155{
44c440bc 156 BT_LIB_LOGD("Destroying packet: %!+a", packet);
312c056a 157
44c440bc 158 if (packet->context_field) {
312c056a
PP
159 if (packet->stream) {
160 BT_LOGD_STR("Recycling packet's context field.");
44c440bc
PP
161 recycle_context_field(packet->context_field,
162 packet->stream->class);
312c056a 163 } else {
44c440bc 164 bt_field_wrapper_destroy(packet->context_field);
312c056a 165 }
238b7404
PP
166
167 packet->context_field = NULL;
312c056a
PP
168 }
169
d409daba 170 BT_LOGD_STR("Putting packet's stream.");
238b7404 171 BT_OBJECT_PUT_REF_AND_RESET(packet->stream);
f79cf0f0
PP
172 g_free(packet);
173}
174
312c056a
PP
175BT_HIDDEN
176struct bt_packet *bt_packet_new(struct bt_stream *stream)
f79cf0f0 177{
50842bdc 178 struct bt_packet *packet = NULL;
862ca4ed 179 struct bt_trace_class *trace_class = NULL;
f79cf0f0 180
312c056a 181 BT_ASSERT(stream);
44c440bc 182 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream);
50842bdc 183 packet = g_new0(struct bt_packet, 1);
f79cf0f0 184 if (!packet) {
870631a2
PP
185 BT_LIB_LOGE_APPEND_CAUSE(
186 "Failed to allocate one packet object.");
44c440bc 187 goto error;
f79cf0f0
PP
188 }
189
3fea54f6
PP
190 bt_object_init_shared(&packet->base,
191 (bt_object_release_func) bt_packet_recycle);
398454ed
PP
192 packet->stream = stream;
193 bt_object_get_no_null_check(stream);
862ca4ed
PP
194 trace_class = bt_stream_class_borrow_trace_class_inline(stream->class);
195 BT_ASSERT(trace_class);
be514b0c 196
5cd6d0e5 197 if (stream->class->packet_context_fc) {
44c440bc
PP
198 BT_LOGD_STR("Creating initial packet context field.");
199 packet->context_field = bt_field_wrapper_create(
200 &stream->class->packet_context_field_pool,
5cd6d0e5 201 stream->class->packet_context_fc);
44c440bc 202 if (!packet->context_field) {
870631a2
PP
203 BT_LIB_LOGE_APPEND_CAUSE(
204 "Cannot create packet context field wrapper.");
44c440bc 205 goto error;
be514b0c 206 }
f79cf0f0
PP
207 }
208
44c440bc
PP
209 BT_LIB_LOGD("Created packet object: %!+a", packet);
210 goto end;
ccf82993 211
44c440bc 212error:
65300d60 213 BT_OBJECT_PUT_REF_AND_RESET(packet);
ccf82993
PP
214
215end:
44c440bc 216 return packet;
ccf82993
PP
217}
218
58085ca4 219struct bt_packet *bt_packet_create(const struct bt_stream *c_stream)
312c056a
PP
220{
221 struct bt_packet *packet = NULL;
58085ca4 222 struct bt_stream *stream = (void *) c_stream;
312c056a
PP
223
224 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
26fc5aed
PP
225 BT_ASSERT_PRE(stream->class->supports_packets,
226 "Stream class does not support packets: %![sc-]+S",
227 stream->class);
312c056a 228 packet = bt_object_pool_create_object(&stream->packet_pool);
91d81473 229 if (G_UNLIKELY(!packet)) {
870631a2
PP
230 BT_LIB_LOGE_APPEND_CAUSE(
231 "Cannot allocate one packet from stream's packet pool: "
312c056a 232 "%![stream-]+s", stream);
6c677fb5 233 goto end;
312c056a
PP
234 }
235
91d81473 236 if (G_LIKELY(!packet->stream)) {
6c677fb5
PP
237 packet->stream = stream;
238 bt_object_get_no_null_check_no_parent_check(
cb6f1f7d 239 &packet->stream->base);
312c056a
PP
240 }
241
312c056a 242end:
e5be10ef 243 return (void *) packet;
312c056a
PP
244}
245
d24d5663
PP
246enum bt_packet_move_context_field_status bt_packet_move_context_field(
247 struct bt_packet *packet,
40f4ba76 248 struct bt_packet_context_field *context_field)
312c056a
PP
249{
250 struct bt_stream_class *stream_class;
251 struct bt_field_wrapper *field_wrapper = (void *) context_field;
252
44c440bc 253 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
312c056a 254 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Context field");
e22b45d0 255 BT_ASSERT_PRE_HOT(packet, "Packet", ": %!+a", packet);
44c440bc 256 stream_class = packet->stream->class;
5cd6d0e5 257 BT_ASSERT_PRE(stream_class->packet_context_fc,
e6276565 258 "Stream class has no packet context field class: %!+S",
312c056a 259 stream_class);
5cd6d0e5
PP
260 BT_ASSERT_PRE(field_wrapper->field->class ==
261 stream_class->packet_context_fc,
83ebb7f1 262 "Unexpected packet context field's class: "
5cd6d0e5
PP
263 "%![fc-]+F, %![expected-fc-]+F", field_wrapper->field->class,
264 stream_class->packet_context_fc);
312c056a
PP
265
266 /* Recycle current context field: always exists */
44c440bc
PP
267 BT_ASSERT(packet->context_field);
268 recycle_context_field(packet->context_field, stream_class);
312c056a
PP
269
270 /* Move new field */
44c440bc 271 packet->context_field = field_wrapper;
d24d5663 272 return BT_FUNC_STATUS_OK;
312c056a 273}
e22b45d0 274
c5b9b441
PP
275void bt_packet_get_ref(const struct bt_packet *packet)
276{
277 bt_object_get_ref(packet);
278}
279
280void bt_packet_put_ref(const struct bt_packet *packet)
281{
282 bt_object_put_ref(packet);
283}
This page took 0.073253 seconds and 4 git commands to generate.