| 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 || packet->frozen) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | BT_LOGD("Freezing packet: addr=%p", packet); |
| 72 | |
| 73 | if (packet->header) { |
| 74 | BT_LOGD_STR("Freezing packet's header field."); |
| 75 | bt_field_set_is_frozen_recursive((void *) packet->header->field, |
| 76 | is_frozen); |
| 77 | } |
| 78 | |
| 79 | if (packet->context) { |
| 80 | BT_LOGD_STR("Freezing packet's context field."); |
| 81 | bt_field_set_is_frozen_recursive((void *) packet->context->field, |
| 82 | is_frozen); |
| 83 | } |
| 84 | |
| 85 | packet->frozen = is_frozen; |
| 86 | } |
| 87 | |
| 88 | static inline |
| 89 | void bt_packet_reset(struct bt_packet *packet) |
| 90 | { |
| 91 | BT_ASSERT(packet); |
| 92 | bt_packet_set_is_frozen(packet, false); |
| 93 | |
| 94 | if (packet->header) { |
| 95 | bt_field_set_is_frozen_recursive( |
| 96 | (void *) packet->header->field, false); |
| 97 | bt_field_reset_recursive((void *) packet->header->field); |
| 98 | } |
| 99 | |
| 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); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static |
| 108 | void bt_packet_header_field_recycle(struct bt_field_wrapper *header_field, |
| 109 | struct bt_trace *trace) |
| 110 | { |
| 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, |
| 116 | header_field); |
| 117 | } |
| 118 | |
| 119 | static |
| 120 | void bt_packet_context_field_recycle(struct bt_field_wrapper *context_field, |
| 121 | struct bt_stream_class *stream_class) |
| 122 | { |
| 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, |
| 128 | context_field); |
| 129 | } |
| 130 | |
| 131 | BT_HIDDEN |
| 132 | void bt_packet_recycle(struct bt_packet *packet) |
| 133 | { |
| 134 | struct bt_stream *stream; |
| 135 | |
| 136 | BT_ASSERT(packet); |
| 137 | BT_LIB_LOGD("Recycling packet: %!+a", packet); |
| 138 | |
| 139 | /* |
| 140 | * Those are the important ordered steps: |
| 141 | * |
| 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. |
| 148 | * |
| 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). |
| 156 | * |
| 157 | * 3. Recycle the packet object. |
| 158 | * |
| 159 | * 4. Put our stream reference. |
| 160 | */ |
| 161 | bt_packet_reset(packet); |
| 162 | stream = packet->stream; |
| 163 | BT_ASSERT(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); |
| 167 | } |
| 168 | |
| 169 | BT_HIDDEN |
| 170 | void bt_packet_destroy(struct bt_packet *packet) |
| 171 | { |
| 172 | BT_LOGD("Destroying packet: addr=%p", packet); |
| 173 | BT_LOGD_STR("Destroying packet's header field."); |
| 174 | |
| 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))); |
| 181 | } else { |
| 182 | bt_field_wrapper_destroy(packet->header); |
| 183 | } |
| 184 | } |
| 185 | |
| 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)); |
| 191 | } else { |
| 192 | bt_field_wrapper_destroy(packet->context); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | BT_LOGD_STR("Putting packet's stream."); |
| 197 | bt_put(packet->stream); |
| 198 | g_free(packet); |
| 199 | } |
| 200 | |
| 201 | BT_HIDDEN |
| 202 | struct bt_packet *bt_packet_new(struct bt_stream *stream) |
| 203 | { |
| 204 | struct bt_packet *packet = NULL; |
| 205 | struct bt_stream_class *stream_class = NULL; |
| 206 | struct bt_trace *trace = NULL; |
| 207 | |
| 208 | BT_ASSERT(stream); |
| 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); |
| 219 | BT_ASSERT(trace); |
| 220 | packet = g_new0(struct bt_packet, 1); |
| 221 | if (!packet) { |
| 222 | BT_LOGE_STR("Failed to allocate one packet object."); |
| 223 | goto end; |
| 224 | } |
| 225 | |
| 226 | bt_object_init_shared(&packet->base, |
| 227 | (bt_object_release_func) bt_packet_recycle); |
| 228 | packet->stream = bt_get(stream); |
| 229 | |
| 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."); |
| 238 | BT_PUT(packet); |
| 239 | goto end; |
| 240 | } |
| 241 | } |
| 242 | |
| 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."); |
| 251 | BT_PUT(packet); |
| 252 | goto end; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | BT_LOGD("Created packet object: addr=%p", packet); |
| 257 | |
| 258 | end: |
| 259 | return packet; |
| 260 | } |
| 261 | |
| 262 | struct bt_packet *bt_packet_create(struct bt_stream *stream) |
| 263 | { |
| 264 | struct bt_packet *packet = NULL; |
| 265 | |
| 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); |
| 271 | goto end; |
| 272 | } |
| 273 | |
| 274 | if (unlikely(!packet->stream)) { |
| 275 | packet->stream = stream; |
| 276 | bt_object_get_no_null_check_no_parent_check( |
| 277 | &packet->stream->common.base); |
| 278 | } |
| 279 | |
| 280 | goto end; |
| 281 | |
| 282 | end: |
| 283 | return packet; |
| 284 | } |
| 285 | |
| 286 | int bt_packet_move_header(struct bt_packet *packet, |
| 287 | struct bt_packet_header_field *header_field) |
| 288 | { |
| 289 | struct bt_trace *trace; |
| 290 | struct bt_field_wrapper *field_wrapper = (void *) header_field; |
| 291 | |
| 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", |
| 299 | trace); |
| 300 | |
| 301 | /* TODO: compare field types (precondition) */ |
| 302 | |
| 303 | /* Recycle current header field: always exists */ |
| 304 | BT_ASSERT(packet->header); |
| 305 | bt_packet_header_field_recycle(packet->header, trace); |
| 306 | |
| 307 | /* Move new field */ |
| 308 | packet->header = field_wrapper; |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | int bt_packet_move_context(struct bt_packet *packet, |
| 313 | struct bt_packet_context_field *context_field) |
| 314 | { |
| 315 | struct bt_stream_class *stream_class; |
| 316 | struct bt_field_wrapper *field_wrapper = (void *) context_field; |
| 317 | |
| 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", |
| 324 | stream_class); |
| 325 | |
| 326 | /* TODO: compare field types (precondition) */ |
| 327 | |
| 328 | /* Recycle current context field: always exists */ |
| 329 | BT_ASSERT(packet->context); |
| 330 | bt_packet_context_field_recycle(packet->context, stream_class); |
| 331 | |
| 332 | /* Move new field */ |
| 333 | packet->context = field_wrapper; |
| 334 | return 0; |
| 335 | } |