2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "LIB/MSG-STREAM-ACTIVITY"
24 #include "lib/logging.h"
26 #include "lib/assert-pre.h"
27 #include "lib/object.h"
28 #include "compat/compiler.h"
29 #include <babeltrace2/trace-ir/clock-class.h>
30 #include "lib/trace-ir/clock-snapshot.h"
31 #include "lib/trace-ir/stream-class.h"
32 #include "lib/trace-ir/stream.h"
33 #include "lib/graph/message/message.h"
34 #include <babeltrace2/graph/message-stream-activity-beginning-const.h>
35 #include <babeltrace2/graph/message-stream-activity-end-const.h>
36 #include <babeltrace2/graph/message-stream-activity-beginning.h>
37 #include <babeltrace2/graph/message-stream-activity-end.h>
39 #include "stream-activity.h"
42 void destroy_stream_activity_message(struct bt_object
*obj
)
44 struct bt_message_stream_activity
*message
= (void *) obj
;
46 BT_LIB_LOGD("Destroying stream activity message: %!+n", message
);
47 BT_LIB_LOGD("Putting stream: %!+s", message
->stream
);
48 BT_OBJECT_PUT_REF_AND_RESET(message
->stream
);
50 if (message
->default_cs
) {
51 bt_clock_snapshot_recycle(message
->default_cs
);
52 message
->default_cs
= NULL
;
59 struct bt_message
*create_stream_activity_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_activity
*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 activity 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_activity
, 1);
75 BT_LIB_LOGE_APPEND_CAUSE(
76 "Failed to allocate one stream activity message.");
80 bt_message_init(&message
->parent
, type
,
81 destroy_stream_activity_message
, NULL
);
82 message
->stream
= stream
;
83 bt_object_get_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 message
->default_cs_state
=
94 BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
;
95 BT_LIB_LOGD("Created stream activity message object: "
96 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message
,
97 stream
, stream_class
);
99 return (void *) &message
->parent
;
105 struct bt_message
*bt_message_stream_activity_beginning_create(
106 struct bt_self_message_iterator
*self_msg_iter
,
107 const struct bt_stream
*stream
)
109 return create_stream_activity_message(self_msg_iter
, (void *) stream
,
110 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
);
113 struct bt_message
*bt_message_stream_activity_end_create(
114 struct bt_self_message_iterator
*self_msg_iter
,
115 const struct bt_stream
*stream
)
117 return create_stream_activity_message(self_msg_iter
, (void *) stream
,
118 BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
);
122 struct bt_stream
*borrow_stream_activity_message_stream(
123 struct bt_message
*message
)
125 struct bt_message_stream_activity
*stream_act_msg
= (void *) message
;
128 return stream_act_msg
->stream
;
131 struct bt_stream
*bt_message_stream_activity_beginning_borrow_stream(
132 struct bt_message
*message
)
134 BT_ASSERT_PRE_NON_NULL(message
, "Message");
135 BT_ASSERT_PRE_MSG_IS_TYPE(message
,
136 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
);
137 return borrow_stream_activity_message_stream(message
);
140 struct bt_stream
*bt_message_stream_activity_end_borrow_stream(
141 struct bt_message
*message
)
143 BT_ASSERT_PRE_NON_NULL(message
, "Message");
144 BT_ASSERT_PRE_MSG_IS_TYPE(message
,
145 BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
);
146 return borrow_stream_activity_message_stream(message
);
149 const struct bt_stream
*bt_message_stream_activity_beginning_borrow_stream_const(
150 const struct bt_message
*message
)
152 return bt_message_stream_activity_beginning_borrow_stream(
156 const struct bt_stream
*bt_message_stream_activity_end_borrow_stream_const(
157 const struct bt_message
*message
)
159 return bt_message_stream_activity_end_borrow_stream((void *) message
);
163 void set_stream_activity_message_default_clock_snapshot(
164 struct bt_message
*msg
, uint64_t value_cycles
)
166 struct bt_message_stream_activity
*stream_act_msg
= (void *) msg
;
167 struct bt_stream_class
*sc
;
170 BT_ASSERT_PRE_HOT(msg
, "Message", ": %!+n", msg
);
171 sc
= stream_act_msg
->stream
->class;
173 BT_ASSERT_PRE(sc
->default_clock_class
,
174 "Message's stream's class has no default clock class: "
175 "%![msg-]+n, %![sc-]+S", msg
, sc
);
176 bt_clock_snapshot_set_raw_value(stream_act_msg
->default_cs
,
178 stream_act_msg
->default_cs_state
=
179 BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
;
180 BT_LIB_LOGD("Set stream activity message's default clock snapshot: "
181 "%![msg-]+n, value=%" PRIu64
, msg
, value_cycles
);
184 void bt_message_stream_activity_beginning_set_default_clock_snapshot(
185 struct bt_message
*msg
, uint64_t raw_value
)
187 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
188 BT_ASSERT_PRE_MSG_IS_TYPE(msg
,
189 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
);
190 set_stream_activity_message_default_clock_snapshot(msg
, raw_value
);
193 void bt_message_stream_activity_end_set_default_clock_snapshot(
194 struct bt_message
*msg
, uint64_t raw_value
)
196 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
197 BT_ASSERT_PRE_MSG_IS_TYPE(msg
,
198 BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
);
199 set_stream_activity_message_default_clock_snapshot(msg
, raw_value
);
203 enum bt_message_stream_activity_clock_snapshot_state
204 borrow_stream_activity_message_default_clock_snapshot_const(
205 const bt_message
*msg
, const bt_clock_snapshot
**snapshot
)
207 const struct bt_message_stream_activity
*stream_act_msg
=
210 BT_ASSERT_PRE_NON_NULL(snapshot
, "Clock snapshot (output)");
211 *snapshot
= stream_act_msg
->default_cs
;
212 return stream_act_msg
->default_cs_state
;
215 enum bt_message_stream_activity_clock_snapshot_state
216 bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
217 const bt_message
*msg
, const bt_clock_snapshot
**snapshot
)
219 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
220 BT_ASSERT_PRE_MSG_IS_TYPE(msg
,
221 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
);
222 return borrow_stream_activity_message_default_clock_snapshot_const(msg
,
226 enum bt_message_stream_activity_clock_snapshot_state
227 bt_message_stream_activity_end_borrow_default_clock_snapshot_const(
228 const bt_message
*msg
, const bt_clock_snapshot
**snapshot
)
230 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
231 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
);
232 return borrow_stream_activity_message_default_clock_snapshot_const(msg
,
237 void set_stream_activity_message_default_clock_snapshot_state(
238 struct bt_message
*msg
,
239 enum bt_message_stream_activity_clock_snapshot_state state
)
241 struct bt_message_stream_activity
*stream_act_msg
= (void *) msg
;
244 BT_ASSERT_PRE_HOT(msg
, "Message", ": %!+n", msg
);
245 BT_ASSERT_PRE(state
!= BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
,
246 "Invalid clock snapshot state: %![msg-]+n, state=%s",
248 bt_message_stream_activity_clock_snapshot_state_string(state
));
249 stream_act_msg
->default_cs_state
= state
;
250 BT_LIB_LOGD("Set stream activity message's default clock snapshot state: "
251 "%![msg-]+n, state=%s", msg
,
252 bt_message_stream_activity_clock_snapshot_state_string(state
));
255 void bt_message_stream_activity_beginning_set_default_clock_snapshot_state(
256 struct bt_message
*msg
,
257 enum bt_message_stream_activity_clock_snapshot_state state
)
259 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
260 BT_ASSERT_PRE_MSG_IS_TYPE(msg
,
261 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
);
262 set_stream_activity_message_default_clock_snapshot_state(msg
, state
);
265 void bt_message_stream_activity_end_set_default_clock_snapshot_state(
266 struct bt_message
*msg
,
267 enum bt_message_stream_activity_clock_snapshot_state state
)
269 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
270 BT_ASSERT_PRE_MSG_IS_TYPE(msg
,
271 BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
);
272 set_stream_activity_message_default_clock_snapshot_state(msg
, state
);
276 const struct bt_clock_class
*
277 borrow_stream_activity_message_stream_class_default_clock_class(
278 const struct bt_message
*msg
)
280 struct bt_message_stream_activity
*stream_act_msg
= (void *) msg
;
283 return stream_act_msg
->stream
->class->default_clock_class
;
286 const struct bt_clock_class
*
287 bt_message_stream_activity_beginning_borrow_stream_class_default_clock_class_const(
288 const struct bt_message
*msg
)
290 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
291 BT_ASSERT_PRE_MSG_IS_TYPE(msg
,
292 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
);
293 return borrow_stream_activity_message_stream_class_default_clock_class(
297 const struct bt_clock_class
*
298 bt_message_stream_activity_end_borrow_stream_class_default_clock_class_const(
299 const struct bt_message
*msg
)
301 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
302 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
);
303 return borrow_stream_activity_message_stream_class_default_clock_class(