2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/MSG-STREAM"
9 #include "lib/logging.h"
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"
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)
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)
32 void destroy_stream_message(struct bt_object
*obj
)
34 struct bt_message_stream
*message
= (void *) obj
;
36 BT_LIB_LOGD("Destroying stream message: %!+n", message
);
38 if (message
->default_cs
) {
39 BT_LIB_LOGD("Putting default clock snapshot: %!+k",
41 bt_clock_snapshot_destroy(message
->default_cs
);
42 message
->default_cs
= NULL
;
45 BT_LIB_LOGD("Putting stream: %!+s", message
->stream
);
46 BT_OBJECT_PUT_REF_AND_RESET(message
->stream
);
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
,
56 struct bt_message_stream
*message
;
57 struct bt_stream_class
*stream_class
;
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);
68 BT_LIB_LOGE_APPEND_CAUSE(
69 "Failed to allocate one stream message.");
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
);
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
) {
86 BT_LIB_LOGD("Created stream message object: "
87 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message
,
88 stream
, stream_class
);
99 return &message
->parent
;
103 struct bt_message
*bt_message_stream_beginning_create(
104 struct bt_self_message_iterator
*self_msg_iter
,
105 const struct bt_stream
*stream
)
107 BT_ASSERT_PRE_DEV_NO_ERROR();
109 return create_stream_message(self_msg_iter
, (void *) stream
,
110 BT_MESSAGE_TYPE_STREAM_BEGINNING
, __func__
);
114 struct bt_message
*bt_message_stream_end_create(
115 struct bt_self_message_iterator
*self_msg_iter
,
116 const struct bt_stream
*stream
)
118 BT_ASSERT_PRE_DEV_NO_ERROR();
120 return create_stream_message(self_msg_iter
, (void *) stream
,
121 BT_MESSAGE_TYPE_STREAM_END
, __func__
);
125 struct bt_stream
*borrow_stream_message_stream(struct bt_message
*message
)
127 struct bt_message_stream
*stream_msg
;
129 BT_ASSERT_DBG(message
);
130 stream_msg
= (void *) message
;
131 return stream_msg
->stream
;
135 struct bt_stream
*bt_message_stream_beginning_borrow_stream(
136 struct bt_message
*message
)
138 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message
);
139 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(message
);
140 return borrow_stream_message_stream(message
);
144 struct bt_stream
*bt_message_stream_end_borrow_stream(
145 struct bt_message
*message
)
147 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message
);
148 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(message
);
149 return borrow_stream_message_stream(message
);
153 const struct bt_stream
*bt_message_stream_beginning_borrow_stream_const(
154 const struct bt_message
*message
)
156 return bt_message_stream_beginning_borrow_stream(
161 const struct bt_stream
*bt_message_stream_end_borrow_stream_const(
162 const struct bt_message
*message
)
164 return bt_message_stream_end_borrow_stream(
169 void set_stream_default_clock_snapshot(
170 struct bt_message
*msg
, uint64_t raw_value
,
171 const char *api_func
)
173 struct bt_message_stream
*stream_msg
= (void *) msg
;
174 struct bt_stream_class
*sc
;
177 BT_ASSERT_PRE_DEV_HOT_FROM_FUNC(api_func
, "message", msg
,
178 "Message", ": %!+n", msg
);
179 sc
= stream_msg
->stream
->class;
181 BT_ASSERT_PRE_MSG_SC_DEF_CLK_CLS_FROM_FUNC(api_func
, msg
, sc
);
182 BT_ASSERT(stream_msg
->default_cs
);
183 bt_clock_snapshot_set_raw_value(stream_msg
->default_cs
, raw_value
);
184 stream_msg
->default_cs_state
= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
;
185 BT_LIB_LOGD("Set stream message's default clock snapshot: "
186 "%![msg-]+n, value=%" PRIu64
, msg
, raw_value
);
190 void bt_message_stream_beginning_set_default_clock_snapshot(
191 struct bt_message
*message
, uint64_t raw_value
)
193 BT_ASSERT_PRE_MSG_NON_NULL(message
);
194 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(message
);
195 set_stream_default_clock_snapshot(message
, raw_value
, __func__
);
199 void bt_message_stream_end_set_default_clock_snapshot(
200 struct bt_message
*message
, uint64_t raw_value
)
202 BT_ASSERT_PRE_MSG_NON_NULL(message
);
203 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(message
);
204 return set_stream_default_clock_snapshot(message
, raw_value
, __func__
);
207 static enum bt_message_stream_clock_snapshot_state
208 borrow_stream_message_default_clock_snapshot_const(
209 const bt_message
*msg
, const bt_clock_snapshot
**snapshot
,
210 const char *api_func
)
212 struct bt_message_stream
*stream_msg
= (void *) msg
;
213 struct bt_stream_class
*sc
;
216 sc
= stream_msg
->stream
->class;
218 BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS_FROM_FUNC(api_func
, msg
, sc
);
219 BT_ASSERT_DBG(stream_msg
->default_cs
);
221 *snapshot
= stream_msg
->default_cs
;
223 return stream_msg
->default_cs_state
;
227 enum bt_message_stream_clock_snapshot_state
228 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
229 const bt_message
*message
, const bt_clock_snapshot
**snapshot
)
231 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message
);
232 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(message
);
233 return borrow_stream_message_default_clock_snapshot_const(
234 message
, snapshot
, __func__
);
238 enum bt_message_stream_clock_snapshot_state
239 bt_message_stream_end_borrow_default_clock_snapshot_const(
240 const bt_message
*message
, const bt_clock_snapshot
**snapshot
)
242 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message
);
243 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(message
);
244 return borrow_stream_message_default_clock_snapshot_const(
245 message
, snapshot
, __func__
);
249 const struct bt_clock_class
*
250 borrow_stream_message_stream_class_default_clock_class(
251 const struct bt_message
*msg
)
253 struct bt_message_stream
*stream_msg
= (void *) msg
;
256 return stream_msg
->stream
->class->default_clock_class
;
260 const struct bt_clock_class
*
261 bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(
262 const struct bt_message
*msg
)
264 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg
);
265 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(msg
);
266 return borrow_stream_message_stream_class_default_clock_class(msg
);
270 const struct bt_clock_class
*
271 bt_message_stream_end_borrow_stream_class_default_clock_class_const(
272 const struct bt_message
*msg
)
274 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg
);
275 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(msg
);
276 return borrow_stream_message_stream_class_default_clock_class(msg
);