| 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-STREAM" |
| 26 | #include <babeltrace/lib-logging-internal.h> |
| 27 | |
| 28 | #include <babeltrace/assert-pre-internal.h> |
| 29 | #include <babeltrace/compiler-internal.h> |
| 30 | #include <babeltrace/trace-ir/stream-internal.h> |
| 31 | #include <babeltrace/trace-ir/stream-class.h> |
| 32 | #include <babeltrace/trace-ir/stream-class-internal.h> |
| 33 | #include <babeltrace/graph/private-notification-stream.h> |
| 34 | #include <babeltrace/graph/notification-stream-internal.h> |
| 35 | #include <babeltrace/graph/private-connection-private-notification-iterator.h> |
| 36 | #include <babeltrace/assert-internal.h> |
| 37 | #include <babeltrace/object.h> |
| 38 | #include <inttypes.h> |
| 39 | |
| 40 | static |
| 41 | void bt_notification_stream_end_destroy(struct bt_object *obj) |
| 42 | { |
| 43 | struct bt_notification_stream_end *notification = |
| 44 | (struct bt_notification_stream_end *) obj; |
| 45 | |
| 46 | BT_LOGD("Destroying stream end notification: addr=%p", |
| 47 | notification); |
| 48 | BT_LOGD_STR("Putting stream."); |
| 49 | BT_OBJECT_PUT_REF_AND_RESET(notification->stream); |
| 50 | |
| 51 | if (notification->default_cv) { |
| 52 | bt_clock_value_recycle(notification->default_cv); |
| 53 | } |
| 54 | |
| 55 | g_free(notification); |
| 56 | } |
| 57 | |
| 58 | struct bt_private_notification *bt_private_notification_stream_end_create( |
| 59 | struct bt_private_connection_private_notification_iterator *notif_iter, |
| 60 | struct bt_private_stream *priv_stream) |
| 61 | { |
| 62 | struct bt_stream *stream = (void *) priv_stream; |
| 63 | struct bt_notification_stream_end *notification; |
| 64 | struct bt_stream_class *stream_class; |
| 65 | |
| 66 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
| 67 | stream_class = bt_stream_borrow_class(stream); |
| 68 | BT_ASSERT(stream_class); |
| 69 | BT_LOGD("Creating stream end notification object: " |
| 70 | "stream-addr=%p, stream-name=\"%s\", " |
| 71 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 72 | "stream-class-id=%" PRId64, |
| 73 | stream, bt_stream_get_name(stream), |
| 74 | stream_class, |
| 75 | bt_stream_class_get_name(stream_class), |
| 76 | bt_stream_class_get_id(stream_class)); |
| 77 | notification = g_new0(struct bt_notification_stream_end, 1); |
| 78 | if (!notification) { |
| 79 | BT_LOGE_STR("Failed to allocate one stream end notification."); |
| 80 | goto error; |
| 81 | } |
| 82 | |
| 83 | bt_notification_init(¬ification->parent, |
| 84 | BT_NOTIFICATION_TYPE_STREAM_END, |
| 85 | bt_notification_stream_end_destroy, NULL); |
| 86 | notification->stream = bt_object_get_ref(stream); |
| 87 | BT_LOGD("Created stream end notification object: " |
| 88 | "stream-addr=%p, stream-name=\"%s\", " |
| 89 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 90 | "stream-class-id=%" PRId64 ", addr=%p", |
| 91 | stream, bt_stream_get_name(stream), |
| 92 | stream_class, |
| 93 | bt_stream_class_get_name(stream_class), |
| 94 | bt_stream_class_get_id(stream_class), notification); |
| 95 | |
| 96 | return (void *) ¬ification->parent; |
| 97 | error: |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | struct bt_stream *bt_notification_stream_end_borrow_stream( |
| 102 | struct bt_notification *notification) |
| 103 | { |
| 104 | struct bt_notification_stream_end *stream_end; |
| 105 | |
| 106 | BT_ASSERT_PRE_NON_NULL(notification, "Notification"); |
| 107 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, |
| 108 | BT_NOTIFICATION_TYPE_STREAM_END); |
| 109 | stream_end = container_of(notification, |
| 110 | struct bt_notification_stream_end, parent); |
| 111 | return stream_end->stream; |
| 112 | } |
| 113 | |
| 114 | struct bt_private_stream *bt_private_notification_stream_end_borrow_stream( |
| 115 | struct bt_private_notification *notification) |
| 116 | { |
| 117 | return (void *) bt_notification_stream_end_borrow_stream( |
| 118 | (void *) notification); |
| 119 | } |
| 120 | |
| 121 | int bt_private_notification_stream_end_set_default_clock_value( |
| 122 | struct bt_private_notification *priv_notif, |
| 123 | uint64_t value_cycles) |
| 124 | { |
| 125 | int ret = 0; |
| 126 | struct bt_notification *notif = (void *) priv_notif; |
| 127 | struct bt_notification_stream_end *se_notif = (void *) notif; |
| 128 | |
| 129 | BT_ASSERT_PRE_NON_NULL(notif, "Notification"); |
| 130 | BT_ASSERT_PRE_HOT(notif, "Notification", ": %!+n", notif); |
| 131 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notif, BT_NOTIFICATION_TYPE_STREAM_END); |
| 132 | BT_ASSERT_PRE(se_notif->stream->class->default_clock_class, |
| 133 | "Notification's stream class has no default clock class: " |
| 134 | "%![notif-]+n, %![sc-]+S", notif, se_notif->stream->class); |
| 135 | |
| 136 | if (!se_notif->default_cv) { |
| 137 | se_notif->default_cv = bt_clock_value_create( |
| 138 | se_notif->stream->class->default_clock_class); |
| 139 | if (!se_notif->default_cv) { |
| 140 | ret = -1; |
| 141 | goto end; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | bt_clock_value_set_value_inline(se_notif->default_cv, value_cycles); |
| 146 | BT_LIB_LOGV("Set notification's default clock value: %![notif-]+n, " |
| 147 | "value=%" PRIu64, value_cycles); |
| 148 | |
| 149 | end: |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | struct bt_clock_value *bt_notification_stream_end_borrow_default_clock_value( |
| 154 | struct bt_notification *notif) |
| 155 | { |
| 156 | struct bt_notification_stream_end *stream_end = (void *) notif; |
| 157 | |
| 158 | BT_ASSERT_PRE_NON_NULL(notif, "Notification"); |
| 159 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notif, BT_NOTIFICATION_TYPE_STREAM_END); |
| 160 | return stream_end->default_cv; |
| 161 | } |
| 162 | |
| 163 | static |
| 164 | void bt_notification_stream_begin_destroy(struct bt_object *obj) |
| 165 | { |
| 166 | struct bt_notification_stream_begin *notification = |
| 167 | (struct bt_notification_stream_begin *) obj; |
| 168 | |
| 169 | BT_LOGD("Destroying stream beginning notification: addr=%p", |
| 170 | notification); |
| 171 | BT_LOGD_STR("Putting stream."); |
| 172 | BT_OBJECT_PUT_REF_AND_RESET(notification->stream); |
| 173 | |
| 174 | if (notification->default_cv) { |
| 175 | bt_clock_value_recycle(notification->default_cv); |
| 176 | } |
| 177 | |
| 178 | g_free(notification); |
| 179 | } |
| 180 | |
| 181 | struct bt_private_notification *bt_private_notification_stream_begin_create( |
| 182 | struct bt_private_connection_private_notification_iterator *notif_iter, |
| 183 | struct bt_private_stream *priv_stream) |
| 184 | { |
| 185 | struct bt_stream *stream = (void *) priv_stream; |
| 186 | struct bt_notification_stream_begin *notification; |
| 187 | struct bt_stream_class *stream_class; |
| 188 | |
| 189 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
| 190 | stream_class = bt_stream_borrow_class(stream); |
| 191 | BT_ASSERT(stream_class); |
| 192 | BT_LOGD("Creating stream beginning notification object: " |
| 193 | "stream-addr=%p, stream-name=\"%s\", " |
| 194 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 195 | "stream-class-id=%" PRId64, |
| 196 | stream, bt_stream_get_name(stream), |
| 197 | stream_class, |
| 198 | bt_stream_class_get_name(stream_class), |
| 199 | bt_stream_class_get_id(stream_class)); |
| 200 | notification = g_new0(struct bt_notification_stream_begin, 1); |
| 201 | if (!notification) { |
| 202 | BT_LOGE_STR("Failed to allocate one stream beginning notification."); |
| 203 | goto error; |
| 204 | } |
| 205 | |
| 206 | bt_notification_init(¬ification->parent, |
| 207 | BT_NOTIFICATION_TYPE_STREAM_BEGIN, |
| 208 | bt_notification_stream_begin_destroy, NULL); |
| 209 | notification->stream = bt_object_get_ref(stream); |
| 210 | BT_LOGD("Created stream beginning notification object: " |
| 211 | "stream-addr=%p, stream-name=\"%s\", " |
| 212 | "stream-class-addr=%p, stream-class-name=\"%s\", " |
| 213 | "stream-class-id=%" PRId64 ", addr=%p", |
| 214 | stream, bt_stream_get_name(stream), |
| 215 | stream_class, |
| 216 | bt_stream_class_get_name(stream_class), |
| 217 | bt_stream_class_get_id(stream_class), notification); |
| 218 | return (void *) ¬ification->parent; |
| 219 | error: |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | struct bt_stream *bt_notification_stream_begin_borrow_stream( |
| 224 | struct bt_notification *notification) |
| 225 | { |
| 226 | struct bt_notification_stream_begin *stream_begin; |
| 227 | |
| 228 | BT_ASSERT_PRE_NON_NULL(notification, "Notification"); |
| 229 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, |
| 230 | BT_NOTIFICATION_TYPE_STREAM_BEGIN); |
| 231 | stream_begin = container_of(notification, |
| 232 | struct bt_notification_stream_begin, parent); |
| 233 | return stream_begin->stream; |
| 234 | } |
| 235 | |
| 236 | struct bt_private_stream *bt_private_notification_stream_begin_borrow_stream( |
| 237 | struct bt_private_notification *notification) |
| 238 | { |
| 239 | return (void *) bt_notification_stream_begin_borrow_stream( |
| 240 | (void *) notification); |
| 241 | } |
| 242 | |
| 243 | int bt_private_notification_stream_begin_set_default_clock_value( |
| 244 | struct bt_private_notification *priv_notif, |
| 245 | uint64_t value_cycles) |
| 246 | { |
| 247 | int ret = 0; |
| 248 | struct bt_notification *notif = (void *) priv_notif; |
| 249 | struct bt_notification_stream_begin *sb_notif = (void *) notif; |
| 250 | |
| 251 | BT_ASSERT_PRE_NON_NULL(notif, "Notification"); |
| 252 | BT_ASSERT_PRE_HOT(notif, "Notification", ": %!+n", notif); |
| 253 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notif, BT_NOTIFICATION_TYPE_STREAM_BEGIN); |
| 254 | BT_ASSERT_PRE(sb_notif->stream->class->default_clock_class, |
| 255 | "Notification's stream class has no default clock class: " |
| 256 | "%![notif-]+n, %![sc-]+S", notif, sb_notif->stream->class); |
| 257 | |
| 258 | if (!sb_notif->default_cv) { |
| 259 | sb_notif->default_cv = bt_clock_value_create( |
| 260 | sb_notif->stream->class->default_clock_class); |
| 261 | if (!sb_notif->default_cv) { |
| 262 | ret = -1; |
| 263 | goto end; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | bt_clock_value_set_value_inline(sb_notif->default_cv, value_cycles); |
| 268 | BT_LIB_LOGV("Set notification's default clock value: %![notif-]+n, " |
| 269 | "value=%" PRIu64, value_cycles); |
| 270 | |
| 271 | end: |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | struct bt_clock_value *bt_notification_stream_begin_borrow_default_clock_value( |
| 276 | struct bt_notification *notif) |
| 277 | { |
| 278 | struct bt_notification_stream_begin *stream_begin = (void *) notif; |
| 279 | |
| 280 | BT_ASSERT_PRE_NON_NULL(notif, "Notification"); |
| 281 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notif, BT_NOTIFICATION_TYPE_STREAM_BEGIN); |
| 282 | return stream_begin->default_cv; |
| 283 | } |