_bt_packet_set_is_frozen(): fix logging statements
[babeltrace.git] / lib / ctf-ir / packet.c
1 /*
2 * packet.c
3 *
4 * Babeltrace CTF IR - Stream packet
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
27 #define BT_LOG_TAG "PACKET"
28 #include <babeltrace/lib-logging-internal.h>
29
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>
44 #include <inttypes.h>
45
46 struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet)
47 {
48 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
49 return packet->stream;
50 }
51
52 struct bt_field *bt_packet_borrow_header(struct bt_packet *packet)
53 {
54 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
55 return packet->header ? (void *) packet->header->field : NULL;
56 }
57
58 struct bt_field *bt_packet_borrow_context(struct bt_packet *packet)
59 {
60 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
61 return packet->context ? (void *) packet->context->field : NULL;
62 }
63
64 BT_HIDDEN
65 void _bt_packet_set_is_frozen(struct bt_packet *packet, bool is_frozen)
66 {
67 if (!packet) {
68 return;
69 }
70
71 BT_LOGD("Setting packet's frozen state: addr=%p, frozen=%d",
72 packet, is_frozen);
73
74 if (packet->header) {
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,
78 is_frozen);
79 }
80
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,
85 is_frozen);
86 }
87
88 packet->frozen = is_frozen;
89 }
90
91 static inline
92 void bt_packet_reset(struct bt_packet *packet)
93 {
94 BT_ASSERT(packet);
95 bt_packet_set_is_frozen(packet, false);
96
97 if (packet->header) {
98 bt_field_set_is_frozen_recursive(
99 (void *) packet->header->field, false);
100 bt_field_reset_recursive((void *) packet->header->field);
101 }
102
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);
107 }
108
109 bt_clock_value_set_reset(&packet->begin_cv_set);
110 bt_clock_value_set_reset(&packet->end_cv_set);
111 }
112
113 static
114 void bt_packet_header_field_recycle(struct bt_field_wrapper *header_field,
115 struct bt_trace *trace)
116 {
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,
122 header_field);
123 }
124
125 static
126 void bt_packet_context_field_recycle(struct bt_field_wrapper *context_field,
127 struct bt_stream_class *stream_class)
128 {
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,
134 context_field);
135 }
136
137 BT_HIDDEN
138 void bt_packet_recycle(struct bt_packet *packet)
139 {
140 struct bt_stream *stream;
141
142 BT_ASSERT(packet);
143 BT_LIB_LOGD("Recycling packet: %!+a", packet);
144
145 /*
146 * Those are the important ordered steps:
147 *
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.
154 *
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).
162 *
163 * 3. Recycle the packet object.
164 *
165 * 4. Put our stream reference.
166 */
167 bt_packet_reset(packet);
168 stream = packet->stream;
169 BT_ASSERT(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);
173 }
174
175 BT_HIDDEN
176 void bt_packet_destroy(struct bt_packet *packet)
177 {
178 BT_LOGD("Destroying packet: addr=%p", packet);
179 BT_LOGD_STR("Destroying packet's header field.");
180
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)));
187 } else {
188 bt_field_wrapper_destroy(packet->header);
189 }
190 }
191
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));
197 } else {
198 bt_field_wrapper_destroy(packet->context);
199 }
200 }
201
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);
206 g_free(packet);
207 }
208
209 BT_HIDDEN
210 struct bt_packet *bt_packet_new(struct bt_stream *stream)
211 {
212 struct bt_packet *packet = NULL;
213 struct bt_stream_class *stream_class = NULL;
214 struct bt_trace *trace = NULL;
215
216 BT_ASSERT(stream);
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);
227 BT_ASSERT(trace);
228 packet = g_new0(struct bt_packet, 1);
229 if (!packet) {
230 BT_LOGE_STR("Failed to allocate one packet object.");
231 goto end;
232 }
233
234 bt_object_init_shared(&packet->base,
235 (bt_object_release_func) bt_packet_recycle);
236 packet->stream = bt_get(stream);
237
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.");
246 BT_PUT(packet);
247 goto end;
248 }
249 }
250
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.");
259 BT_PUT(packet);
260 goto end;
261 }
262 }
263
264 if (bt_clock_value_set_initialize(&packet->begin_cv_set)) {
265 BT_PUT(packet);
266 goto end;
267 }
268
269 if (bt_clock_value_set_initialize(&packet->end_cv_set)) {
270 BT_PUT(packet);
271 goto end;
272 }
273
274 BT_LOGD("Created packet object: addr=%p", packet);
275
276 end:
277 return packet;
278 }
279
280 struct bt_packet *bt_packet_create(struct bt_stream *stream)
281 {
282 struct bt_packet *packet = NULL;
283
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);
289 goto end;
290 }
291
292 if (unlikely(!packet->stream)) {
293 packet->stream = stream;
294 bt_object_get_no_null_check_no_parent_check(
295 &packet->stream->common.base);
296 }
297
298 goto end;
299
300 end:
301 return packet;
302 }
303
304 int bt_packet_move_header(struct bt_packet *packet,
305 struct bt_packet_header_field *header_field)
306 {
307 struct bt_trace *trace;
308 struct bt_field_wrapper *field_wrapper = (void *) header_field;
309
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",
317 trace);
318
319 /* TODO: compare field types (precondition) */
320
321 /* Recycle current header field: always exists */
322 BT_ASSERT(packet->header);
323 bt_packet_header_field_recycle(packet->header, trace);
324
325 /* Move new field */
326 packet->header = field_wrapper;
327 return 0;
328 }
329
330 int bt_packet_move_context(struct bt_packet *packet,
331 struct bt_packet_context_field *context_field)
332 {
333 struct bt_stream_class *stream_class;
334 struct bt_field_wrapper *field_wrapper = (void *) context_field;
335
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",
342 stream_class);
343
344 /* TODO: compare field types (precondition) */
345
346 /* Recycle current context field: always exists */
347 BT_ASSERT(packet->context);
348 bt_packet_context_field_recycle(packet->context, stream_class);
349
350 /* Move new field */
351 packet->context = field_wrapper;
352 return 0;
353 }
354
355 int bt_packet_set_beginning_clock_value(struct bt_packet *packet,
356 struct bt_clock_class *clock_class, uint64_t raw_value,
357 bt_bool is_default)
358 {
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);
366 }
367
368 struct bt_clock_value *bt_packet_borrow_default_begin_clock_value(
369 struct bt_packet *packet)
370 {
371 struct bt_clock_value *clock_value = NULL;
372
373 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
374 clock_value = packet->begin_cv_set.default_cv;
375 if (!clock_value) {
376 BT_LIB_LOGV("No default clock value: %![packet-]+a", packet);
377 }
378
379 return clock_value;
380 }
381
382 int bt_packet_set_end_clock_value(struct bt_packet *packet,
383 struct bt_clock_class *clock_class, uint64_t raw_value,
384 bt_bool is_default)
385 {
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);
393 }
394
395 struct bt_clock_value *bt_packet_borrow_default_end_clock_value(
396 struct bt_packet *packet)
397 {
398 struct bt_clock_value *clock_value = NULL;
399
400 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
401 clock_value = packet->end_cv_set.default_cv;
402 if (!clock_value) {
403 BT_LIB_LOGV("No default clock value: %![packet-]+a", packet);
404 }
405
406 return clock_value;
407 }
This page took 0.038586 seconds and 5 git commands to generate.