Rename: bt_put(), bt_get() -> bt_object_put_ref(), bt_object_get_ref()
[babeltrace.git] / lib / trace-ir / packet.c
CommitLineData
f79cf0f0
PP
1/*
2 * packet.c
3 *
108b91d0 4 * Babeltrace trace IR - Stream packet
f79cf0f0
PP
5 *
6 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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
24 * SOFTWARE.
25 */
26
8ea705f0 27#define BT_LOG_TAG "PACKET"
be514b0c 28#include <babeltrace/lib-logging-internal.h>
8ea705f0 29
8deee039 30#include <babeltrace/assert-pre-internal.h>
108b91d0
PP
31#include <babeltrace/trace-ir/fields-internal.h>
32#include <babeltrace/trace-ir/packet.h>
33#include <babeltrace/trace-ir/packet-internal.h>
34#include <babeltrace/trace-ir/field-wrapper-internal.h>
35#include <babeltrace/trace-ir/trace.h>
36#include <babeltrace/trace-ir/stream-class-internal.h>
37#include <babeltrace/trace-ir/stream-class.h>
38#include <babeltrace/trace-ir/stream.h>
39#include <babeltrace/trace-ir/stream-internal.h>
40#include <babeltrace/trace-ir/clock-value-internal.h>
41#include <babeltrace/trace-ir/trace-internal.h>
f79cf0f0 42#include <babeltrace/object-internal.h>
8138bfe1 43#include <babeltrace/object.h>
8b45963b 44#include <babeltrace/assert-internal.h>
be514b0c 45#include <inttypes.h>
f79cf0f0 46
7b33a0e0
PP
47#define BT_ASSERT_PRE_PACKET_HOT(_packet) \
48 BT_ASSERT_PRE_HOT((_packet), "Packet", ": %!+a", (_packet))
49
5fe68922 50struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet)
f79cf0f0 51{
8b45963b 52 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
5fe68922 53 return packet->stream;
f79cf0f0
PP
54}
55
7b33a0e0 56struct bt_field *bt_packet_borrow_header_field(struct bt_packet *packet)
f79cf0f0 57{
8b45963b 58 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
7b33a0e0 59 return packet->header_field ? packet->header_field->field : NULL;
f79cf0f0
PP
60}
61
7b33a0e0 62struct bt_field *bt_packet_borrow_context_field(struct bt_packet *packet)
f79cf0f0 63{
a6918753 64 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
7b33a0e0 65 return packet->context_field ? packet->context_field->field : NULL;
a6918753 66}
c5c82c6e 67
a6918753 68BT_HIDDEN
c5a24b0a 69void _bt_packet_set_is_frozen(struct bt_packet *packet, bool is_frozen)
a6918753 70{
435ec8af 71 if (!packet) {
a6918753 72 return;
c5c82c6e
PP
73 }
74
7b33a0e0
PP
75 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
76 "is-frozen=%d", packet, is_frozen);
a6918753 77
7b33a0e0
PP
78 if (packet->header_field) {
79 BT_LOGD_STR("Setting packet's header field's frozen state.");
80 bt_field_set_is_frozen(packet->header_field->field,
c5a24b0a 81 is_frozen);
f79cf0f0
PP
82 }
83
7b33a0e0
PP
84 if (packet->context_field) {
85 BT_LOGD_STR("Setting packet's context field's frozen state.");
86 bt_field_set_is_frozen(packet->context_field->field,
c5a24b0a 87 is_frozen);
a6918753
PP
88 }
89
c5a24b0a 90 packet->frozen = is_frozen;
8b45963b
PP
91}
92
e5815ba2 93static inline
7b33a0e0 94void reset_counter_snapshots(struct bt_packet *packet)
e5815ba2 95{
7b33a0e0
PP
96 packet->discarded_event_counter_snapshot.base.avail =
97 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
98 packet->packet_counter_snapshot.base.avail =
99 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
e5815ba2
PP
100}
101
a6918753 102static inline
7b33a0e0 103void reset_packet(struct bt_packet *packet)
8b45963b 104{
a6918753 105 BT_ASSERT(packet);
7b33a0e0 106 BT_LIB_LOGD("Resetting packet: %!+a", packet);
c5a24b0a 107 bt_packet_set_is_frozen(packet, false);
a6918753 108
7b33a0e0
PP
109 if (packet->header_field) {
110 bt_field_set_is_frozen(packet->header_field->field, false);
111 bt_field_reset(packet->header_field->field);
a6918753
PP
112 }
113
7b33a0e0
PP
114 if (packet->context_field) {
115 bt_field_set_is_frozen(packet->context_field->field, false);
116 bt_field_reset(packet->context_field->field);
a6918753 117 }
8fc063a2 118
7b33a0e0
PP
119 if (packet->default_beginning_cv) {
120 bt_clock_value_reset(packet->default_beginning_cv);
121 }
e5815ba2 122
7b33a0e0
PP
123 if (packet->default_end_cv) {
124 bt_clock_value_reset(packet->default_end_cv);
e5815ba2 125 }
7b33a0e0
PP
126
127 reset_counter_snapshots(packet);
f79cf0f0
PP
128}
129
a6918753 130static
7b33a0e0 131void recycle_header_field(struct bt_field_wrapper *header_field,
a6918753 132 struct bt_trace *trace)
f79cf0f0 133{
a6918753
PP
134 BT_ASSERT(header_field);
135 BT_LIB_LOGD("Recycling packet header field: "
136 "addr=%p, %![trace-]+t, %![field-]+f", header_field,
137 trace, header_field->field);
138 bt_object_pool_recycle_object(&trace->packet_header_field_pool,
139 header_field);
f79cf0f0
PP
140}
141
a6918753 142static
7b33a0e0 143void recycle_context_field(struct bt_field_wrapper *context_field,
a6918753 144 struct bt_stream_class *stream_class)
f79cf0f0 145{
a6918753
PP
146 BT_ASSERT(context_field);
147 BT_LIB_LOGD("Recycling packet context field: "
148 "addr=%p, %![sc-]+S, %![field-]+f", context_field,
149 stream_class, context_field->field);
150 bt_object_pool_recycle_object(&stream_class->packet_context_field_pool,
151 context_field);
f79cf0f0
PP
152}
153
154BT_HIDDEN
a6918753 155void bt_packet_recycle(struct bt_packet *packet)
f79cf0f0 156{
a6918753 157 struct bt_stream *stream;
f79cf0f0 158
a6918753
PP
159 BT_ASSERT(packet);
160 BT_LIB_LOGD("Recycling packet: %!+a", packet);
161
162 /*
163 * Those are the important ordered steps:
164 *
165 * 1. Reset the packet object (put any permanent reference it
166 * has, unfreeze it and its fields in developer mode, etc.),
167 * but do NOT put its stream's reference. This stream
168 * contains the pool to which we're about to recycle this
169 * packet object, so we must guarantee its existence thanks
170 * to this existing reference.
171 *
172 * 2. Move the stream reference to our `stream`
173 * variable so that we can set the packet's stream member
174 * to NULL before recycling it. We CANNOT do this after
8138bfe1 175 * we put the stream reference because this bt_object_put_ref()
a6918753
PP
176 * could destroy the stream, also destroying its
177 * packet pool, thus also destroying our packet object (this
178 * would result in an invalid write access).
179 *
180 * 3. Recycle the packet object.
181 *
182 * 4. Put our stream reference.
183 */
7b33a0e0 184 reset_packet(packet);
a6918753
PP
185 stream = packet->stream;
186 BT_ASSERT(stream);
187 packet->stream = NULL;
188 bt_object_pool_recycle_object(&stream->packet_pool, packet);
18acc6f8 189 bt_object_put_no_null_check(&stream->base);
f79cf0f0
PP
190}
191
a6918753
PP
192BT_HIDDEN
193void bt_packet_destroy(struct bt_packet *packet)
f79cf0f0 194{
7b33a0e0 195 BT_LIB_LOGD("Destroying packet: %!+a", packet);
a6918753 196
7b33a0e0 197 if (packet->header_field) {
a6918753
PP
198 if (packet->stream) {
199 BT_LOGD_STR("Recycling packet's header field.");
7b33a0e0
PP
200 recycle_header_field(packet->header_field,
201 bt_stream_class_borrow_trace_inline(
202 packet->stream->class));
a6918753 203 } else {
7b33a0e0 204 bt_field_wrapper_destroy(packet->header_field);
a6918753
PP
205 }
206 }
207
7b33a0e0 208 if (packet->context_field) {
a6918753
PP
209 if (packet->stream) {
210 BT_LOGD_STR("Recycling packet's context field.");
7b33a0e0
PP
211 recycle_context_field(packet->context_field,
212 packet->stream->class);
a6918753 213 } else {
7b33a0e0 214 bt_field_wrapper_destroy(packet->context_field);
a6918753
PP
215 }
216 }
217
7b33a0e0
PP
218 if (packet->default_beginning_cv) {
219 BT_LOGD_STR("Recycling beginning clock value.");
220 bt_clock_value_recycle(packet->default_beginning_cv);
221 }
222
223 if (packet->default_end_cv) {
224 BT_LOGD_STR("Recycling end clock value.");
225 bt_clock_value_recycle(packet->default_end_cv);
226 }
227
d409daba 228 BT_LOGD_STR("Putting packet's stream.");
8138bfe1 229 bt_object_put_ref(packet->stream);
f79cf0f0
PP
230 g_free(packet);
231}
232
a6918753
PP
233BT_HIDDEN
234struct bt_packet *bt_packet_new(struct bt_stream *stream)
f79cf0f0 235{
839d52a5 236 struct bt_packet *packet = NULL;
839d52a5 237 struct bt_trace *trace = NULL;
f79cf0f0 238
a6918753 239 BT_ASSERT(stream);
7b33a0e0 240 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream);
839d52a5 241 packet = g_new0(struct bt_packet, 1);
f79cf0f0 242 if (!packet) {
8ea705f0 243 BT_LOGE_STR("Failed to allocate one packet object.");
7b33a0e0 244 goto error;
f79cf0f0
PP
245 }
246
1d7bf349
PP
247 bt_object_init_shared(&packet->base,
248 (bt_object_release_func) bt_packet_recycle);
8138bfe1 249 packet->stream = bt_object_get_ref(stream);
7b33a0e0
PP
250 trace = bt_stream_class_borrow_trace_inline(stream->class);
251 BT_ASSERT(trace);
be514b0c 252
939190b3 253 if (trace->packet_header_fc) {
7b33a0e0
PP
254 BT_LOGD_STR("Creating initial packet header field.");
255 packet->header_field = bt_field_wrapper_create(
a6918753 256 &trace->packet_header_field_pool,
939190b3 257 trace->packet_header_fc);
7b33a0e0
PP
258 if (!packet->header_field) {
259 BT_LOGE_STR("Cannot create packet header field wrapper.");
260 goto error;
be514b0c 261 }
f79cf0f0
PP
262 }
263
939190b3 264 if (stream->class->packet_context_fc) {
7b33a0e0
PP
265 BT_LOGD_STR("Creating initial packet context field.");
266 packet->context_field = bt_field_wrapper_create(
267 &stream->class->packet_context_field_pool,
939190b3 268 stream->class->packet_context_fc);
7b33a0e0
PP
269 if (!packet->context_field) {
270 BT_LOGE_STR("Cannot create packet context field wrapper.");
271 goto error;
be514b0c 272 }
f79cf0f0
PP
273 }
274
7b33a0e0
PP
275 if (stream->class->default_clock_class) {
276 if (stream->class->packets_have_default_beginning_cv) {
277 packet->default_beginning_cv = bt_clock_value_create(
278 stream->class->default_clock_class);
279 if (!packet->default_beginning_cv) {
280 /* bt_clock_value_create() logs errors */
281 goto error;
282 }
e5815ba2
PP
283 }
284
7b33a0e0
PP
285 if (stream->class->packets_have_default_end_cv) {
286 packet->default_end_cv = bt_clock_value_create(
287 stream->class->default_clock_class);
288 if (!packet->default_end_cv) {
289 /* bt_clock_value_create() logs errors */
290 goto error;
291 }
e5815ba2
PP
292 }
293 }
294
7b33a0e0
PP
295 reset_counter_snapshots(packet);
296 BT_LIB_LOGD("Created packet object: %!+a", packet);
297 goto end;
e5815ba2 298
7b33a0e0 299error:
8138bfe1 300 BT_OBJECT_PUT_REF_AND_RESET(packet);
e5815ba2
PP
301
302end:
7b33a0e0 303 return packet;
e5815ba2
PP
304}
305
7b33a0e0 306struct bt_packet *bt_packet_create(struct bt_stream *stream)
a6918753
PP
307{
308 struct bt_packet *packet = NULL;
309
310 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
311 packet = bt_object_pool_create_object(&stream->packet_pool);
c5a24b0a 312 if (unlikely(!packet)) {
a6918753
PP
313 BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
314 "%![stream-]+s", stream);
c5a24b0a 315 goto end;
a6918753
PP
316 }
317
7b33a0e0 318 if (likely(!packet->stream)) {
c5a24b0a
PP
319 packet->stream = stream;
320 bt_object_get_no_null_check_no_parent_check(
18acc6f8 321 &packet->stream->base);
a6918753
PP
322 }
323
a6918753
PP
324end:
325 return packet;
326}
327
7b33a0e0 328int bt_packet_move_header_field(struct bt_packet *packet,
a6918753
PP
329 struct bt_packet_header_field *header_field)
330{
331 struct bt_trace *trace;
332 struct bt_field_wrapper *field_wrapper = (void *) header_field;
333
7b33a0e0 334 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
a6918753 335 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Header field");
7b33a0e0
PP
336 BT_ASSERT_PRE_PACKET_HOT(packet);
337 trace = bt_stream_class_borrow_trace_inline(packet->stream->class);
939190b3
PP
338 BT_ASSERT_PRE(trace->packet_header_fc,
339 "Trace has no packet header field classe: %!+t",
a6918753 340 trace);
939190b3
PP
341 BT_ASSERT_PRE(field_wrapper->field->class ==
342 trace->packet_header_fc,
343 "Unexpected packet header field's class: "
344 "%![fc-]+F, %![expected-fc-]+F", field_wrapper->field->class,
345 trace->packet_header_fc);
a6918753
PP
346
347 /* Recycle current header field: always exists */
7b33a0e0
PP
348 BT_ASSERT(packet->header_field);
349 recycle_header_field(packet->header_field, trace);
a6918753
PP
350
351 /* Move new field */
7b33a0e0 352 packet->header_field = field_wrapper;
a6918753
PP
353 return 0;
354}
355
7b33a0e0 356int bt_packet_move_context_field(struct bt_packet *packet,
a6918753
PP
357 struct bt_packet_context_field *context_field)
358{
359 struct bt_stream_class *stream_class;
360 struct bt_field_wrapper *field_wrapper = (void *) context_field;
361
7b33a0e0 362 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
a6918753 363 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Context field");
8fc063a2 364 BT_ASSERT_PRE_HOT(packet, "Packet", ": %!+a", packet);
7b33a0e0 365 stream_class = packet->stream->class;
939190b3
PP
366 BT_ASSERT_PRE(stream_class->packet_context_fc,
367 "Stream class has no packet context field classe: %!+S",
a6918753 368 stream_class);
939190b3
PP
369 BT_ASSERT_PRE(field_wrapper->field->class ==
370 stream_class->packet_context_fc,
371 "Unexpected packet header field's class: "
372 "%![fc-]+F, %![expected-fc-]+F", field_wrapper->field->class,
373 stream_class->packet_context_fc);
a6918753
PP
374
375 /* Recycle current context field: always exists */
7b33a0e0
PP
376 BT_ASSERT(packet->context_field);
377 recycle_context_field(packet->context_field, stream_class);
a6918753
PP
378
379 /* Move new field */
7b33a0e0 380 packet->context_field = field_wrapper;
a6918753
PP
381 return 0;
382}
8fc063a2 383
7b33a0e0
PP
384int bt_packet_set_default_beginning_clock_value(struct bt_packet *packet,
385 uint64_t value_cycles)
8fc063a2 386{
7b33a0e0 387 struct bt_stream_class *sc;
e5815ba2 388
8fc063a2 389 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
7b33a0e0
PP
390 BT_ASSERT_PRE_PACKET_HOT(packet);
391 sc = packet->stream->class;
392 BT_ASSERT(sc);
393 BT_ASSERT_PRE(sc->default_clock_class,
394 "Packet's stream class has no default clock class: "
395 "%![packet-]+a, %![sc-]+S", packet, sc);
396 BT_ASSERT_PRE(sc->packets_have_default_beginning_cv,
397 "Packet's stream class indicates that its packets have "
398 "no default beginning clock value: %![packet-]+a, %![sc-]+S",
399 packet, sc);
400 BT_ASSERT(packet->default_beginning_cv);
401 bt_clock_value_set_value_inline(packet->default_beginning_cv, value_cycles);
402 BT_LIB_LOGV("Set packet's default beginning clock value: "
403 "%![packet-]+a, value=%" PRIu64, value_cycles);
404 return 0;
8fc063a2
PP
405}
406
7b33a0e0
PP
407enum bt_clock_value_status bt_packet_borrow_default_beginning_clock_value(
408 struct bt_packet *packet, struct bt_clock_value **clock_value)
8fc063a2 409{
8fc063a2 410 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
7b33a0e0
PP
411 BT_ASSERT_PRE_NON_NULL(clock_value, "Clock value (output)");
412 *clock_value = packet->default_beginning_cv;
413 return BT_CLOCK_VALUE_STATUS_KNOWN;
8fc063a2
PP
414}
415
7b33a0e0
PP
416int bt_packet_set_default_end_clock_value(struct bt_packet *packet,
417 uint64_t value_cycles)
8fc063a2 418{
7b33a0e0
PP
419 struct bt_stream_class *sc;
420
8fc063a2 421 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
7b33a0e0
PP
422 BT_ASSERT_PRE_PACKET_HOT(packet);
423 sc = packet->stream->class;
424 BT_ASSERT(sc);
425 BT_ASSERT_PRE(sc->default_clock_class,
426 "Packet's stream class has no default clock class: "
427 "%![packet-]+a, %![sc-]+S", packet, sc);
428 BT_ASSERT_PRE(sc->packets_have_default_end_cv,
429 "Packet's stream class indicates that its packets have "
430 "no default end clock value: %![packet-]+a, %![sc-]+S",
431 packet, sc);
432 BT_ASSERT(packet->default_end_cv);
433 bt_clock_value_set_value_inline(packet->default_end_cv, value_cycles);
434 BT_LIB_LOGV("Set packet's default end clock value: "
435 "%![packet-]+a, value=%" PRIu64, value_cycles);
436 return 0;
e5815ba2
PP
437}
438
7b33a0e0 439enum bt_clock_value_status bt_packet_borrow_default_end_clock_value(
e5815ba2
PP
440 struct bt_packet *packet, struct bt_clock_value **clock_value)
441{
442 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
7b33a0e0
PP
443 BT_ASSERT_PRE_NON_NULL(clock_value, "Clock value (output)");
444 *clock_value = packet->default_end_cv;
445 return BT_CLOCK_VALUE_STATUS_KNOWN;
8fc063a2
PP
446}
447
7b33a0e0
PP
448enum bt_property_availability bt_packet_get_discarded_event_counter_snapshot(
449 struct bt_packet *packet, uint64_t *value)
8fc063a2 450{
e5815ba2 451 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
7b33a0e0
PP
452 BT_ASSERT_PRE_NON_NULL(value, "Value (output)");
453 *value = packet->discarded_event_counter_snapshot.value;
454 return packet->discarded_event_counter_snapshot.base.avail;
e5815ba2 455}
8fc063a2 456
7b33a0e0
PP
457int bt_packet_set_discarded_event_counter_snapshot(struct bt_packet *packet,
458 uint64_t value)
e5815ba2 459{
8fc063a2 460 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
7b33a0e0
PP
461 BT_ASSERT_PRE_PACKET_HOT(packet);
462 BT_ASSERT_PRE(packet->stream->class->packets_have_discarded_event_counter_snapshot,
463 "Packet's stream's discarded event counter is not enabled: "
464 "%![packet-]+a", packet);
465 bt_property_uint_set(&packet->discarded_event_counter_snapshot, value);
466 return 0;
e5815ba2 467}
8fc063a2 468
7b33a0e0
PP
469enum bt_property_availability bt_packet_get_packet_counter_snapshot(
470 struct bt_packet *packet, uint64_t *value)
e5815ba2
PP
471{
472 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
7b33a0e0
PP
473 BT_ASSERT_PRE_NON_NULL(value, "Value (output)");
474 *value = packet->packet_counter_snapshot.value;
475 return packet->packet_counter_snapshot.base.avail;
e5815ba2
PP
476}
477
7b33a0e0
PP
478int bt_packet_set_packet_counter_snapshot(struct bt_packet *packet,
479 uint64_t value)
e5815ba2
PP
480{
481 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
7b33a0e0
PP
482 BT_ASSERT_PRE_PACKET_HOT(packet);
483 BT_ASSERT_PRE(packet->stream->class->packets_have_packet_counter_snapshot,
484 "Packet's stream's packet counter is not enabled: "
485 "%![packet-]+a", packet);
486 bt_property_uint_set(&packet->packet_counter_snapshot, value);
487 return 0;
8fc063a2 488}
This page took 0.061765 seconds and 4 git commands to generate.