| 1 | /* |
| 2 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #define BT_LOG_TAG "NOTIF-PACKET" |
| 26 | #include <babeltrace/lib-logging-internal.h> |
| 27 | |
| 28 | #include <babeltrace/compiler-internal.h> |
| 29 | #include <babeltrace/trace-ir/packet.h> |
| 30 | #include <babeltrace/trace-ir/packet-internal.h> |
| 31 | #include <babeltrace/trace-ir/stream-class.h> |
| 32 | #include <babeltrace/trace-ir/stream.h> |
| 33 | #include <babeltrace/trace-ir/stream-internal.h> |
| 34 | #include <babeltrace/graph/graph-internal.h> |
| 35 | #include <babeltrace/graph/private-notification-packet.h> |
| 36 | #include <babeltrace/graph/notification-packet-internal.h> |
| 37 | #include <babeltrace/object.h> |
| 38 | #include <babeltrace/assert-internal.h> |
| 39 | #include <babeltrace/assert-pre-internal.h> |
| 40 | #include <babeltrace/object-internal.h> |
| 41 | #include <inttypes.h> |
| 42 | |
| 43 | BT_HIDDEN |
| 44 | struct bt_notification *bt_notification_packet_begin_new(struct bt_graph *graph) |
| 45 | { |
| 46 | struct bt_notification_packet_begin *notification; |
| 47 | |
| 48 | notification = g_new0(struct bt_notification_packet_begin, 1); |
| 49 | if (!notification) { |
| 50 | BT_LOGE_STR("Failed to allocate one packet beginning notification."); |
| 51 | goto error; |
| 52 | } |
| 53 | |
| 54 | bt_notification_init(¬ification->parent, |
| 55 | BT_NOTIFICATION_TYPE_PACKET_BEGIN, |
| 56 | (bt_object_release_func) bt_notification_packet_begin_recycle, |
| 57 | graph); |
| 58 | goto end; |
| 59 | |
| 60 | error: |
| 61 | BT_OBJECT_PUT_REF_AND_RESET(notification); |
| 62 | |
| 63 | end: |
| 64 | return (void *) notification; |
| 65 | } |
| 66 | |
| 67 | struct bt_private_notification *bt_private_notification_packet_begin_create( |
| 68 | struct bt_self_notification_iterator *self_notif_iter, |
| 69 | struct bt_packet *packet) |
| 70 | { |
| 71 | struct bt_self_component_port_input_notification_iterator *notif_iter = |
| 72 | (void *) self_notif_iter; |
| 73 | struct bt_notification_packet_begin *notification = NULL; |
| 74 | struct bt_stream *stream; |
| 75 | struct bt_stream_class *stream_class; |
| 76 | |
| 77 | BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator"); |
| 78 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); |
| 79 | stream = bt_packet_borrow_stream(packet); |
| 80 | BT_ASSERT(stream); |
| 81 | stream_class = bt_stream_borrow_class(stream); |
| 82 | BT_ASSERT(stream_class); |
| 83 | BT_LIB_LOGD("Creating packet beginning notification object: " |
| 84 | "%![packet-]+a, %![stream-]+s, %![sc-]+S", |
| 85 | packet, stream, stream_class); |
| 86 | notification = (void *) bt_notification_create_from_pool( |
| 87 | ¬if_iter->graph->packet_begin_notif_pool, notif_iter->graph); |
| 88 | if (!notification) { |
| 89 | /* bt_notification_create_from_pool() logs errors */ |
| 90 | goto end; |
| 91 | } |
| 92 | |
| 93 | BT_ASSERT(!notification->packet); |
| 94 | notification->packet = packet; |
| 95 | bt_object_get_no_null_check_no_parent_check( |
| 96 | ¬ification->packet->base); |
| 97 | bt_packet_set_is_frozen(packet, true); |
| 98 | BT_LIB_LOGD("Created packet beginning notification object: " |
| 99 | "%![notif-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S", |
| 100 | notification, packet, stream, stream_class); |
| 101 | goto end; |
| 102 | |
| 103 | end: |
| 104 | return (void *) notification; |
| 105 | } |
| 106 | |
| 107 | BT_HIDDEN |
| 108 | void bt_notification_packet_begin_destroy(struct bt_notification *notif) |
| 109 | { |
| 110 | struct bt_notification_packet_begin *packet_begin_notif = (void *) notif; |
| 111 | |
| 112 | BT_LIB_LOGD("Destroying packet beginning notification: %!+n", notif); |
| 113 | BT_LIB_LOGD("Putting packet: %!+a", packet_begin_notif->packet); |
| 114 | BT_OBJECT_PUT_REF_AND_RESET(packet_begin_notif->packet); |
| 115 | g_free(notif); |
| 116 | } |
| 117 | |
| 118 | BT_HIDDEN |
| 119 | void bt_notification_packet_begin_recycle(struct bt_notification *notif) |
| 120 | { |
| 121 | struct bt_notification_packet_begin *packet_begin_notif = (void *) notif; |
| 122 | struct bt_graph *graph; |
| 123 | |
| 124 | BT_ASSERT(packet_begin_notif); |
| 125 | |
| 126 | if (unlikely(!notif->graph)) { |
| 127 | bt_notification_packet_begin_destroy(notif); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | BT_LIB_LOGD("Recycling packet beginning notification: %!+n", notif); |
| 132 | bt_notification_reset(notif); |
| 133 | bt_object_put_no_null_check(&packet_begin_notif->packet->base); |
| 134 | packet_begin_notif->packet = NULL; |
| 135 | graph = notif->graph; |
| 136 | notif->graph = NULL; |
| 137 | bt_object_pool_recycle_object(&graph->packet_begin_notif_pool, notif); |
| 138 | } |
| 139 | |
| 140 | struct bt_packet *bt_private_notification_packet_begin_borrow_packet( |
| 141 | struct bt_private_notification *notification) |
| 142 | { |
| 143 | struct bt_notification_packet_begin *packet_begin; |
| 144 | |
| 145 | BT_ASSERT_PRE_NON_NULL(notification, "Notification"); |
| 146 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, |
| 147 | BT_NOTIFICATION_TYPE_PACKET_BEGIN); |
| 148 | packet_begin = (void *) notification; |
| 149 | return packet_begin->packet; |
| 150 | } |
| 151 | |
| 152 | const struct bt_packet *bt_notification_packet_begin_borrow_packet( |
| 153 | struct bt_notification *notification) |
| 154 | { |
| 155 | return bt_private_notification_packet_begin_borrow_packet( |
| 156 | (void *) notification); |
| 157 | } |
| 158 | |
| 159 | BT_HIDDEN |
| 160 | struct bt_notification *bt_notification_packet_end_new(struct bt_graph *graph) |
| 161 | { |
| 162 | struct bt_notification_packet_end *notification; |
| 163 | |
| 164 | notification = g_new0(struct bt_notification_packet_end, 1); |
| 165 | if (!notification) { |
| 166 | BT_LOGE_STR("Failed to allocate one packet end notification."); |
| 167 | goto error; |
| 168 | } |
| 169 | |
| 170 | bt_notification_init(¬ification->parent, |
| 171 | BT_NOTIFICATION_TYPE_PACKET_END, |
| 172 | (bt_object_release_func) bt_notification_packet_end_recycle, |
| 173 | graph); |
| 174 | goto end; |
| 175 | |
| 176 | error: |
| 177 | BT_OBJECT_PUT_REF_AND_RESET(notification); |
| 178 | |
| 179 | end: |
| 180 | return (void *) notification; |
| 181 | } |
| 182 | |
| 183 | struct bt_private_notification *bt_private_notification_packet_end_create( |
| 184 | struct bt_self_notification_iterator *self_notif_iter, |
| 185 | struct bt_packet *packet) |
| 186 | { |
| 187 | struct bt_self_component_port_input_notification_iterator *notif_iter = |
| 188 | (void *) self_notif_iter; |
| 189 | struct bt_notification_packet_end *notification = NULL; |
| 190 | struct bt_stream *stream; |
| 191 | struct bt_stream_class *stream_class; |
| 192 | |
| 193 | BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator"); |
| 194 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); |
| 195 | stream = bt_packet_borrow_stream(packet); |
| 196 | BT_ASSERT(stream); |
| 197 | stream_class = bt_stream_borrow_class(stream); |
| 198 | BT_ASSERT(stream_class); |
| 199 | BT_LIB_LOGD("Creating packet end notification object: " |
| 200 | "%![packet-]+a, %![stream-]+s, %![sc-]+S", |
| 201 | packet, stream, stream_class); |
| 202 | notification = (void *) bt_notification_create_from_pool( |
| 203 | ¬if_iter->graph->packet_end_notif_pool, notif_iter->graph); |
| 204 | if (!notification) { |
| 205 | /* bt_notification_create_from_pool() logs errors */ |
| 206 | goto end; |
| 207 | } |
| 208 | |
| 209 | BT_ASSERT(!notification->packet); |
| 210 | notification->packet = packet; |
| 211 | bt_object_get_no_null_check_no_parent_check( |
| 212 | ¬ification->packet->base); |
| 213 | bt_packet_set_is_frozen(packet, true); |
| 214 | BT_LIB_LOGD("Created packet end notification object: " |
| 215 | "%![notif-]+n, %![packet-]+a, %![stream-]+s, %![sc-]+S", |
| 216 | notification, packet, stream, stream_class); |
| 217 | goto end; |
| 218 | |
| 219 | end: |
| 220 | return (void *) notification; |
| 221 | } |
| 222 | |
| 223 | BT_HIDDEN |
| 224 | void bt_notification_packet_end_destroy(struct bt_notification *notif) |
| 225 | { |
| 226 | struct bt_notification_packet_end *packet_end_notif = (void *) notif; |
| 227 | |
| 228 | BT_LIB_LOGD("Destroying packet end notification: %!+n", notif); |
| 229 | BT_LIB_LOGD("Putting packet: %!+a", packet_end_notif->packet); |
| 230 | BT_OBJECT_PUT_REF_AND_RESET(packet_end_notif->packet); |
| 231 | g_free(notif); |
| 232 | } |
| 233 | |
| 234 | BT_HIDDEN |
| 235 | void bt_notification_packet_end_recycle(struct bt_notification *notif) |
| 236 | { |
| 237 | struct bt_notification_packet_end *packet_end_notif = (void *) notif; |
| 238 | struct bt_graph *graph; |
| 239 | |
| 240 | BT_ASSERT(packet_end_notif); |
| 241 | |
| 242 | if (!notif->graph) { |
| 243 | bt_notification_packet_end_destroy(notif); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | BT_LIB_LOGD("Recycling packet end notification: %!+n", notif); |
| 248 | bt_notification_reset(notif); |
| 249 | BT_OBJECT_PUT_REF_AND_RESET(packet_end_notif->packet); |
| 250 | graph = notif->graph; |
| 251 | notif->graph = NULL; |
| 252 | bt_object_pool_recycle_object(&graph->packet_end_notif_pool, notif); |
| 253 | } |
| 254 | |
| 255 | struct bt_packet *bt_private_notification_packet_end_borrow_packet( |
| 256 | struct bt_private_notification *notification) |
| 257 | { |
| 258 | struct bt_notification_packet_end *packet_end; |
| 259 | |
| 260 | BT_ASSERT_PRE_NON_NULL(notification, "Notification"); |
| 261 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, |
| 262 | BT_NOTIFICATION_TYPE_PACKET_END); |
| 263 | packet_end = (void *) notification; |
| 264 | return packet_end->packet; |
| 265 | } |
| 266 | |
| 267 | const struct bt_packet *bt_notification_packet_end_borrow_packet( |
| 268 | struct bt_notification *notification) |
| 269 | { |
| 270 | return bt_private_notification_packet_end_borrow_packet( |
| 271 | (void *) notification); |
| 272 | } |