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 "MSG-STREAM-ACTIVITY"
24 #include <babeltrace/lib-logging-internal.h>
26 #include <babeltrace/assert-pre-internal.h>
27 #include <babeltrace/object-internal.h>
28 #include <babeltrace/compiler-internal.h>
29 #include <babeltrace/trace-ir/clock-class.h>
30 #include <babeltrace/trace-ir/clock-snapshot-internal.h>
31 #include <babeltrace/trace-ir/stream-class-internal.h>
32 #include <babeltrace/trace-ir/stream-internal.h>
33 #include <babeltrace/graph/message-internal.h>
34 #include <babeltrace/graph/message-stream-activity-beginning-const.h>
35 #include <babeltrace/graph/message-stream-activity-end-const.h>
36 #include <babeltrace/graph/message-stream-activity-beginning.h>
37 #include <babeltrace/graph/message-stream-activity-end.h>
38 #include <babeltrace/graph/message-stream-activity-internal.h>
41 void destroy_stream_activity_message(struct bt_object
*obj
)
43 struct bt_message_stream_activity
*message
= (void *) obj
;
45 BT_LIB_LOGD("Destroying stream activity message: %!+n", message
);
46 BT_LIB_LOGD("Putting stream: %!+s", message
->stream
);
47 BT_OBJECT_PUT_REF_AND_RESET(message
->stream
);
49 if (message
->default_cs
) {
50 bt_clock_snapshot_recycle(message
->default_cs
);
57 struct bt_message
*create_stream_activity_message(
58 struct bt_self_message_iterator
*self_msg_iter
,
59 struct bt_stream
*stream
, enum bt_message_type type
)
61 struct bt_message_stream_activity
*message
;
62 struct bt_stream_class
*stream_class
;
64 BT_ASSERT_PRE_NON_NULL(self_msg_iter
, "Message iterator");
65 BT_ASSERT_PRE_NON_NULL(stream
, "Stream");
66 stream_class
= bt_stream_borrow_class(stream
);
67 BT_ASSERT(stream_class
);
68 BT_LIB_LOGD("Creating stream activity message object: "
69 "type=%s, %![stream-]+s, %![sc-]+S",
70 bt_message_type_string(type
), stream
, stream_class
);
71 message
= g_new0(struct bt_message_stream_activity
, 1);
73 BT_LOGE_STR("Failed to allocate one stream activity message.");
77 bt_message_init(&message
->parent
, type
,
78 destroy_stream_activity_message
, NULL
);
79 message
->stream
= stream
;
80 bt_object_get_no_null_check(message
->stream
);
82 if (stream_class
->default_clock_class
) {
83 message
->default_cs
= bt_clock_snapshot_create(
84 stream_class
->default_clock_class
);
85 if (!message
->default_cs
) {
90 message
->default_cs_state
=
91 BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
;
92 BT_LIB_LOGD("Created stream activity message object: "
93 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message
,
94 stream
, stream_class
);
96 return (void *) &message
->parent
;
102 struct bt_message
*bt_message_stream_activity_beginning_create(
103 struct bt_self_message_iterator
*self_msg_iter
,
104 struct bt_stream
*stream
)
106 return create_stream_activity_message(self_msg_iter
, stream
,
107 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
);
110 struct bt_message
*bt_message_stream_activity_end_create(
111 struct bt_self_message_iterator
*self_msg_iter
,
112 struct bt_stream
*stream
)
114 return create_stream_activity_message(self_msg_iter
, stream
,
115 BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
);
119 struct bt_stream
*borrow_stream_activity_message_stream(
120 struct bt_message
*message
)
122 struct bt_message_stream_activity
*stream_act_msg
= (void *) message
;
125 return stream_act_msg
->stream
;
128 struct bt_stream
*bt_message_stream_activity_beginning_borrow_stream(
129 struct bt_message
*message
)
131 BT_ASSERT_PRE_NON_NULL(message
, "Message");
132 BT_ASSERT_PRE_MSG_IS_TYPE(message
,
133 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
);
134 return borrow_stream_activity_message_stream(message
);
137 struct bt_stream
*bt_message_stream_activity_end_borrow_stream(
138 struct bt_message
*message
)
140 BT_ASSERT_PRE_NON_NULL(message
, "Message");
141 BT_ASSERT_PRE_MSG_IS_TYPE(message
,
142 BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
);
143 return borrow_stream_activity_message_stream(message
);
146 const struct bt_stream
*bt_message_stream_activity_beginning_borrow_stream_const(
147 const struct bt_message
*message
)
149 return bt_message_stream_activity_beginning_borrow_stream(
153 const struct bt_stream
*bt_message_stream_activity_end_borrow_stream_const(
154 const struct bt_message
*message
)
156 return bt_message_stream_activity_end_borrow_stream((void *) message
);
160 void set_stream_activity_message_default_clock_snapshot(
161 struct bt_message
*msg
, uint64_t value_cycles
)
163 struct bt_message_stream_activity
*stream_act_msg
= (void *) msg
;
164 struct bt_stream_class
*sc
;
167 BT_ASSERT_PRE_HOT(msg
, "Message", ": %!+n", msg
);
168 sc
= stream_act_msg
->stream
->class;
170 BT_ASSERT_PRE(sc
->default_clock_class
,
171 "Message's stream's class has no default clock class: "
172 "%![msg-]+n, %![sc-]+S", msg
, sc
);
173 bt_clock_snapshot_set_raw_value(stream_act_msg
->default_cs
,
175 stream_act_msg
->default_cs_state
=
176 BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
;
177 BT_LIB_LOGV("Set stream activity message's default clock snapshot: "
178 "%![msg-]+n, value=%" PRIu64
, msg
, value_cycles
);
181 void bt_message_stream_activity_beginning_set_default_clock_snapshot(
182 struct bt_message
*msg
, uint64_t raw_value
)
184 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
185 BT_ASSERT_PRE_MSG_IS_TYPE(msg
,
186 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
);
187 set_stream_activity_message_default_clock_snapshot(msg
, raw_value
);
190 void bt_message_stream_activity_end_set_default_clock_snapshot(
191 struct bt_message
*msg
, uint64_t raw_value
)
193 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
194 BT_ASSERT_PRE_MSG_IS_TYPE(msg
,
195 BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
);
196 set_stream_activity_message_default_clock_snapshot(msg
, raw_value
);
200 enum bt_message_stream_activity_clock_snapshot_state
201 borrow_stream_activity_message_default_clock_snapshot_const(
202 const bt_message
*msg
, const bt_clock_snapshot
**snapshot
)
204 const struct bt_message_stream_activity
*stream_act_msg
=
207 BT_ASSERT_PRE_NON_NULL(snapshot
, "Clock snapshot (output)");
208 *snapshot
= stream_act_msg
->default_cs
;
209 return stream_act_msg
->default_cs_state
;
212 enum bt_message_stream_activity_clock_snapshot_state
213 bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
214 const bt_message
*msg
, const bt_clock_snapshot
**snapshot
)
216 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
217 BT_ASSERT_PRE_MSG_IS_TYPE(msg
,
218 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
);
219 return borrow_stream_activity_message_default_clock_snapshot_const(msg
,
223 enum bt_message_stream_activity_clock_snapshot_state
224 bt_message_stream_activity_end_borrow_default_clock_snapshot_const(
225 const bt_message
*msg
, const bt_clock_snapshot
**snapshot
)
227 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
228 BT_ASSERT_PRE_MSG_IS_TYPE(msg
, BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
);
229 return borrow_stream_activity_message_default_clock_snapshot_const(msg
,
234 void set_stream_activity_message_default_clock_snapshot_state(
235 struct bt_message
*msg
,
236 enum bt_message_stream_activity_clock_snapshot_state state
)
238 struct bt_message_stream_activity
*stream_act_msg
= (void *) msg
;
241 BT_ASSERT_PRE_HOT(msg
, "Message", ": %!+n", msg
);
242 BT_ASSERT_PRE(state
!= BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN
,
243 "Invalid clock snapshot state: %![msg-]+n, state=%s",
245 bt_message_stream_activity_clock_snapshot_state_string(state
));
246 stream_act_msg
->default_cs_state
= state
;
247 BT_LIB_LOGV("Set stream activity message's default clock snapshot state: "
248 "%![msg-]+n, state=%s", msg
,
249 bt_message_stream_activity_clock_snapshot_state_string(state
));
252 void bt_message_stream_activity_beginning_set_default_clock_snapshot_state(
253 struct bt_message
*msg
,
254 enum bt_message_stream_activity_clock_snapshot_state state
)
256 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
257 BT_ASSERT_PRE_MSG_IS_TYPE(msg
,
258 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
);
259 set_stream_activity_message_default_clock_snapshot_state(msg
, state
);
262 void bt_message_stream_activity_end_set_default_clock_snapshot_state(
263 struct bt_message
*msg
,
264 enum bt_message_stream_activity_clock_snapshot_state state
)
266 BT_ASSERT_PRE_NON_NULL(msg
, "Message");
267 BT_ASSERT_PRE_MSG_IS_TYPE(msg
,
268 BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
);
269 set_stream_activity_message_default_clock_snapshot_state(msg
, state
);