2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "LIB/MSG-STREAM"
25 #include "lib/logging.h"
27 #include "lib/assert-pre.h"
28 #include "compat/compiler.h"
29 #include <babeltrace2/trace-ir/clock-snapshot.h>
30 #include "lib/trace-ir/stream.h"
31 #include <babeltrace2/trace-ir/stream-class.h>
32 #include "lib/trace-ir/stream-class.h"
33 #include <babeltrace2/graph/message.h>
34 #include "common/assert.h"
40 void destroy_stream_message(struct bt_object
*obj
)
42 struct bt_message_stream
*message
= (void *) obj
;
44 BT_LIB_LOGD("Destroying stream message: %!+n", message
);
46 if (message
->default_cs
) {
47 BT_LIB_LOGD("Putting default clock snapshot: %!+k",
49 bt_clock_snapshot_destroy(message
->default_cs
);
50 message
->default_cs
= NULL
;
53 BT_LIB_LOGD("Putting stream: %!+s", message
->stream
);
54 BT_OBJECT_PUT_REF_AND_RESET(message
->stream
);
59 struct bt_message
*create_stream_message(
60 struct bt_self_message_iterator
*self_msg_iter
,
61 struct bt_stream
*stream
, enum bt_message_type type
)
63 struct bt_message_stream
*message
;
64 struct bt_stream_class
*stream_class
;
66 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
67 BT_ASSERT_PRE_NON_NULL(stream
, "Stream");
68 stream_class
= bt_stream_borrow_class(stream
);
69 BT_ASSERT(stream_class
);
70 BT_LIB_LOGD("Creating stream message object: "
71 "type=%s, %![stream-]+s, %![sc-]+S",
72 bt_message_type_string(type
), stream
, stream_class
);
73 message
= g_new0(struct bt_message_stream
, 1);
75 BT_LIB_LOGE_APPEND_CAUSE(
76 "Failed to allocate one stream message.");
80 bt_message_init(&message
->parent
, type
,
81 destroy_stream_message
, NULL
);
82 message
->stream
= stream
;
83 bt_object_get_ref_no_null_check(message
->stream
);
85 if (stream_class
->default_clock_class
) {
86 message
->default_cs
= bt_clock_snapshot_create(
87 stream_class
->default_clock_class
);
88 if (!message
->default_cs
) {
93 BT_LIB_LOGD("Created stream message object: "
94 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message
,
95 stream
, stream_class
);
106 return &message
->parent
;
109 struct bt_message
*bt_message_stream_beginning_create(
110 struct bt_self_message_iterator
*self_msg_iter
,
111 const struct bt_stream
*stream
)
113 BT_ASSERT_PRE_DEV_NO_ERROR();
115 return create_stream_message(self_msg_iter
, (void *) stream
,
116 BT_MESSAGE_TYPE_STREAM_BEGINNING
);
119 struct bt_message
*bt_message_stream_end_create(
120 struct bt_self_message_iterator
*self_msg_iter
,
121 const struct bt_stream
*stream
)
123 BT_ASSERT_PRE_DEV_NO_ERROR();
125 return create_stream_message(self_msg_iter
, (void *) stream
,
126 BT_MESSAGE_TYPE_STREAM_END
);
130 struct bt_stream
*borrow_stream_message_stream(struct bt_message
*message
)
132 struct bt_message_stream
*stream_msg
;
134 BT_ASSERT_DBG(message
);
135 stream_msg
= (void *) message
;
136 return stream_msg
->stream
;
139 struct bt_stream
*bt_message_stream_beginning_borrow_stream(
140 struct bt_message
*message
)
142 BT_ASSERT_PRE_DEV_NON_NULL(message
, "Message");
143 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message
,
144 BT_MESSAGE_TYPE_STREAM_BEGINNING
);
145 return borrow_stream_message_stream(message
);
148 struct bt_stream
*bt_message_stream_end_borrow_stream(
149 struct bt_message
*message
)
151 BT_ASSERT_PRE_DEV_NON_NULL(message
, "Message");
152 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message
,
153 BT_MESSAGE_TYPE_STREAM_END
);
154 return borrow_stream_message_stream(message
);
157 const struct bt_stream
*bt_message_stream_beginning_borrow_stream_const(
158 const struct bt_message
*message
)
160 return bt_message_stream_beginning_borrow_stream(
164 const struct bt_stream
*bt_message_stream_end_borrow_stream_const(
165 const struct bt_message
*message
)
167 return bt_message_stream_end_borrow_stream(
172 void bt_message_stream_set_default_clock_snapshot(
173 struct bt_message
*msg
, uint64_t raw_value
)
175 struct bt_message_stream
*stream_msg
= (void *) msg
;
176 struct bt_stream_class
*sc
;
179 BT_ASSERT_PRE_DEV_HOT(msg
, "Message", ": %!+n", msg
);
180 sc
= stream_msg
->stream
->class;
182 BT_ASSERT_PRE(sc
->default_clock_class
,
183 "Message's stream's class has no default clock class: "
184 "%![msg-]+n, %![sc-]+S", msg
, sc
);
185 BT_ASSERT(stream_msg
->default_cs
);
186 bt_clock_snapshot_set_raw_value(stream_msg
->default_cs
, raw_value
);
187 stream_msg
->default_cs_state
= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
;
188 BT_LIB_LOGD("Set stream message's default clock snapshot: "
189 "%![msg-]+n, value=%" PRIu64
, msg
, raw_value
);
192 void bt_message_stream_beginning_set_default_clock_snapshot(
193 struct bt_message
*message
, uint64_t raw_value
)
195 BT_ASSERT_PRE_NON_NULL(message
, "Message");
196 BT_ASSERT_PRE_MSG_IS_TYPE(message
, BT_MESSAGE_TYPE_STREAM_BEGINNING
);
198 bt_message_stream_set_default_clock_snapshot(message
, raw_value
);
201 void bt_message_stream_end_set_default_clock_snapshot(
202 struct bt_message
*message
, uint64_t raw_value
)
204 BT_ASSERT_PRE_NON_NULL(message
, "Message");
205 BT_ASSERT_PRE_MSG_IS_TYPE(message
, BT_MESSAGE_TYPE_STREAM_END
);
207 return bt_message_stream_set_default_clock_snapshot(message
, raw_value
);
210 static enum bt_message_stream_clock_snapshot_state
211 bt_message_stream_borrow_default_clock_snapshot_const(
212 const bt_message
*msg
, const bt_clock_snapshot
**snapshot
)
214 struct bt_message_stream
*stream_msg
= (void *) msg
;
215 struct bt_stream_class
*sc
;
218 sc
= stream_msg
->stream
->class;
220 BT_ASSERT_PRE_DEV(sc
->default_clock_class
,
221 "Message's stream's class has no default clock class: "
222 "%![msg-]+n, %![sc-]+S", msg
, sc
);
223 BT_ASSERT_DBG(stream_msg
->default_cs
);
225 *snapshot
= stream_msg
->default_cs
;
227 return stream_msg
->default_cs_state
;
230 enum bt_message_stream_clock_snapshot_state
231 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
232 const bt_message
*message
, const bt_clock_snapshot
**snapshot
)
234 BT_ASSERT_PRE_DEV_NON_NULL(message
, "Message");
235 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message
,
236 BT_MESSAGE_TYPE_STREAM_BEGINNING
);
237 return bt_message_stream_borrow_default_clock_snapshot_const(
241 enum bt_message_stream_clock_snapshot_state
242 bt_message_stream_end_borrow_default_clock_snapshot_const(
243 const bt_message
*message
, const bt_clock_snapshot
**snapshot
)
245 BT_ASSERT_PRE_DEV_NON_NULL(message
, "Message");
246 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message
, BT_MESSAGE_TYPE_STREAM_END
);
247 return bt_message_stream_borrow_default_clock_snapshot_const(
252 const struct bt_clock_class
*
253 borrow_stream_message_stream_class_default_clock_class(
254 const struct bt_message
*msg
)
256 struct bt_message_stream
*stream_msg
= (void *) msg
;
259 return stream_msg
->stream
->class->default_clock_class
;
262 const struct bt_clock_class
*
263 bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(
264 const struct bt_message
*msg
)
266 BT_ASSERT_PRE_DEV_NON_NULL(msg
, "Message");
267 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg
,
268 BT_MESSAGE_TYPE_STREAM_BEGINNING
);
269 return borrow_stream_message_stream_class_default_clock_class(msg
);
272 const struct bt_clock_class
*
273 bt_message_stream_end_borrow_stream_class_default_clock_class_const(
274 const struct bt_message
*msg
)
276 BT_ASSERT_PRE_DEV_NON_NULL(msg
, "Message");
277 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_STREAM_END
);
278 return borrow_stream_message_stream_class_default_clock_class(msg
);