| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #define BT_LOG_TAG "LIB/MSG-STREAM" |
| 9 | #include "lib/logging.h" |
| 10 | |
| 11 | #include "lib/assert-cond.h" |
| 12 | #include "compat/compiler.h" |
| 13 | #include <babeltrace2/trace-ir/clock-snapshot.h> |
| 14 | #include "lib/trace-ir/stream.h" |
| 15 | #include <babeltrace2/trace-ir/stream-class.h> |
| 16 | #include "lib/trace-ir/stream-class.h" |
| 17 | #include <babeltrace2/graph/message.h> |
| 18 | #include "common/assert.h" |
| 19 | #include <inttypes.h> |
| 20 | |
| 21 | #include "stream.h" |
| 22 | |
| 23 | #define BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(_msg) \ |
| 24 | BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg), \ |
| 25 | "stream-beginning", BT_MESSAGE_TYPE_STREAM_BEGINNING) |
| 26 | |
| 27 | #define BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(_msg) \ |
| 28 | BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg), \ |
| 29 | "stream-end", BT_MESSAGE_TYPE_STREAM_END) |
| 30 | |
| 31 | static |
| 32 | void destroy_stream_message(struct bt_object *obj) |
| 33 | { |
| 34 | struct bt_message_stream *message = (void *) obj; |
| 35 | |
| 36 | BT_LIB_LOGD("Destroying stream message: %!+n", message); |
| 37 | |
| 38 | if (message->default_cs) { |
| 39 | BT_LIB_LOGD("Putting default clock snapshot: %!+k", |
| 40 | message->default_cs); |
| 41 | bt_clock_snapshot_destroy(message->default_cs); |
| 42 | message->default_cs = NULL; |
| 43 | } |
| 44 | |
| 45 | BT_LIB_LOGD("Putting stream: %!+s", message->stream); |
| 46 | BT_OBJECT_PUT_REF_AND_RESET(message->stream); |
| 47 | g_free(message); |
| 48 | } |
| 49 | |
| 50 | static inline |
| 51 | struct bt_message *create_stream_message( |
| 52 | struct bt_self_message_iterator *self_msg_iter, |
| 53 | struct bt_stream *stream, enum bt_message_type type, |
| 54 | const char *api_func) |
| 55 | { |
| 56 | struct bt_message_stream *message; |
| 57 | struct bt_stream_class *stream_class; |
| 58 | |
| 59 | BT_ASSERT_PRE_MSG_ITER_NON_NULL_FROM_FUNC(api_func, self_msg_iter); |
| 60 | BT_ASSERT_PRE_STREAM_NON_NULL_FROM_FUNC(api_func, stream); |
| 61 | stream_class = bt_stream_borrow_class(stream); |
| 62 | BT_ASSERT(stream_class); |
| 63 | BT_LIB_LOGD("Creating stream message object: " |
| 64 | "type=%s, %![stream-]+s, %![sc-]+S", |
| 65 | bt_common_message_type_string(type), stream, stream_class); |
| 66 | message = g_new0(struct bt_message_stream, 1); |
| 67 | if (!message) { |
| 68 | BT_LIB_LOGE_APPEND_CAUSE( |
| 69 | "Failed to allocate one stream message."); |
| 70 | goto error; |
| 71 | } |
| 72 | |
| 73 | bt_message_init(&message->parent, type, |
| 74 | destroy_stream_message, NULL); |
| 75 | message->stream = stream; |
| 76 | bt_object_get_ref_no_null_check(message->stream); |
| 77 | |
| 78 | if (stream_class->default_clock_class) { |
| 79 | message->default_cs = bt_clock_snapshot_create( |
| 80 | stream_class->default_clock_class); |
| 81 | if (!message->default_cs) { |
| 82 | goto error; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | BT_LIB_LOGD("Created stream message object: " |
| 87 | "%![msg-]+n, %![stream-]+s, %![sc-]+S", message, |
| 88 | stream, stream_class); |
| 89 | |
| 90 | goto end; |
| 91 | |
| 92 | error: |
| 93 | if (message) { |
| 94 | g_free(message); |
| 95 | message = NULL; |
| 96 | } |
| 97 | |
| 98 | end: |
| 99 | return &message->parent; |
| 100 | } |
| 101 | |
| 102 | struct bt_message *bt_message_stream_beginning_create( |
| 103 | struct bt_self_message_iterator *self_msg_iter, |
| 104 | const struct bt_stream *stream) |
| 105 | { |
| 106 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 107 | |
| 108 | return create_stream_message(self_msg_iter, (void *) stream, |
| 109 | BT_MESSAGE_TYPE_STREAM_BEGINNING, __func__); |
| 110 | } |
| 111 | |
| 112 | struct bt_message *bt_message_stream_end_create( |
| 113 | struct bt_self_message_iterator *self_msg_iter, |
| 114 | const struct bt_stream *stream) |
| 115 | { |
| 116 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
| 117 | |
| 118 | return create_stream_message(self_msg_iter, (void *) stream, |
| 119 | BT_MESSAGE_TYPE_STREAM_END, __func__); |
| 120 | } |
| 121 | |
| 122 | static inline |
| 123 | struct bt_stream *borrow_stream_message_stream(struct bt_message *message) |
| 124 | { |
| 125 | struct bt_message_stream *stream_msg; |
| 126 | |
| 127 | BT_ASSERT_DBG(message); |
| 128 | stream_msg = (void *) message; |
| 129 | return stream_msg->stream; |
| 130 | } |
| 131 | |
| 132 | struct bt_stream *bt_message_stream_beginning_borrow_stream( |
| 133 | struct bt_message *message) |
| 134 | { |
| 135 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
| 136 | BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(message); |
| 137 | return borrow_stream_message_stream(message); |
| 138 | } |
| 139 | |
| 140 | struct bt_stream *bt_message_stream_end_borrow_stream( |
| 141 | struct bt_message *message) |
| 142 | { |
| 143 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
| 144 | BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(message); |
| 145 | return borrow_stream_message_stream(message); |
| 146 | } |
| 147 | |
| 148 | const struct bt_stream *bt_message_stream_beginning_borrow_stream_const( |
| 149 | const struct bt_message *message) |
| 150 | { |
| 151 | return bt_message_stream_beginning_borrow_stream( |
| 152 | (void *) message); |
| 153 | } |
| 154 | |
| 155 | const struct bt_stream *bt_message_stream_end_borrow_stream_const( |
| 156 | const struct bt_message *message) |
| 157 | { |
| 158 | return bt_message_stream_end_borrow_stream( |
| 159 | (void *) message); |
| 160 | } |
| 161 | |
| 162 | static |
| 163 | void set_stream_default_clock_snapshot( |
| 164 | struct bt_message *msg, uint64_t raw_value, |
| 165 | const char *api_func) |
| 166 | { |
| 167 | struct bt_message_stream *stream_msg = (void *) msg; |
| 168 | struct bt_stream_class *sc; |
| 169 | |
| 170 | BT_ASSERT(msg); |
| 171 | BT_ASSERT_PRE_DEV_HOT_FROM_FUNC(api_func, "message", msg, |
| 172 | "Message", ": %!+n", msg); |
| 173 | sc = stream_msg->stream->class; |
| 174 | BT_ASSERT(sc); |
| 175 | BT_ASSERT_PRE_MSG_SC_DEF_CLK_CLS_FROM_FUNC(api_func, msg, sc); |
| 176 | BT_ASSERT(stream_msg->default_cs); |
| 177 | bt_clock_snapshot_set_raw_value(stream_msg->default_cs, raw_value); |
| 178 | stream_msg->default_cs_state = BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN; |
| 179 | BT_LIB_LOGD("Set stream message's default clock snapshot: " |
| 180 | "%![msg-]+n, value=%" PRIu64, msg, raw_value); |
| 181 | } |
| 182 | |
| 183 | void bt_message_stream_beginning_set_default_clock_snapshot( |
| 184 | struct bt_message *message, uint64_t raw_value) |
| 185 | { |
| 186 | BT_ASSERT_PRE_MSG_NON_NULL(message); |
| 187 | BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(message); |
| 188 | set_stream_default_clock_snapshot(message, raw_value, __func__); |
| 189 | } |
| 190 | |
| 191 | void bt_message_stream_end_set_default_clock_snapshot( |
| 192 | struct bt_message *message, uint64_t raw_value) |
| 193 | { |
| 194 | BT_ASSERT_PRE_MSG_NON_NULL(message); |
| 195 | BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(message); |
| 196 | return set_stream_default_clock_snapshot(message, raw_value, __func__); |
| 197 | } |
| 198 | |
| 199 | static enum bt_message_stream_clock_snapshot_state |
| 200 | borrow_stream_message_default_clock_snapshot_const( |
| 201 | const bt_message *msg, const bt_clock_snapshot **snapshot, |
| 202 | const char *api_func) |
| 203 | { |
| 204 | struct bt_message_stream *stream_msg = (void *) msg; |
| 205 | struct bt_stream_class *sc; |
| 206 | |
| 207 | BT_ASSERT_DBG(msg); |
| 208 | sc = stream_msg->stream->class; |
| 209 | BT_ASSERT_DBG(sc); |
| 210 | BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS_FROM_FUNC(api_func, msg, sc); |
| 211 | BT_ASSERT_DBG(stream_msg->default_cs); |
| 212 | |
| 213 | *snapshot = stream_msg->default_cs; |
| 214 | |
| 215 | return stream_msg->default_cs_state; |
| 216 | } |
| 217 | |
| 218 | enum bt_message_stream_clock_snapshot_state |
| 219 | bt_message_stream_beginning_borrow_default_clock_snapshot_const( |
| 220 | const bt_message *message, const bt_clock_snapshot **snapshot) |
| 221 | { |
| 222 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
| 223 | BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(message); |
| 224 | return borrow_stream_message_default_clock_snapshot_const( |
| 225 | message, snapshot, __func__); |
| 226 | } |
| 227 | |
| 228 | enum bt_message_stream_clock_snapshot_state |
| 229 | bt_message_stream_end_borrow_default_clock_snapshot_const( |
| 230 | const bt_message *message, const bt_clock_snapshot **snapshot) |
| 231 | { |
| 232 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
| 233 | BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(message); |
| 234 | return borrow_stream_message_default_clock_snapshot_const( |
| 235 | message, snapshot, __func__); |
| 236 | } |
| 237 | |
| 238 | static inline |
| 239 | const struct bt_clock_class * |
| 240 | borrow_stream_message_stream_class_default_clock_class( |
| 241 | const struct bt_message *msg) |
| 242 | { |
| 243 | struct bt_message_stream *stream_msg = (void *) msg; |
| 244 | |
| 245 | BT_ASSERT_DBG(msg); |
| 246 | return stream_msg->stream->class->default_clock_class; |
| 247 | } |
| 248 | |
| 249 | const struct bt_clock_class * |
| 250 | bt_message_stream_beginning_borrow_stream_class_default_clock_class_const( |
| 251 | const struct bt_message *msg) |
| 252 | { |
| 253 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 254 | BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(msg); |
| 255 | return borrow_stream_message_stream_class_default_clock_class(msg); |
| 256 | } |
| 257 | |
| 258 | const struct bt_clock_class * |
| 259 | bt_message_stream_end_borrow_stream_class_default_clock_class_const( |
| 260 | const struct bt_message *msg) |
| 261 | { |
| 262 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
| 263 | BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(msg); |
| 264 | return borrow_stream_message_stream_class_default_clock_class(msg); |
| 265 | } |