d9ef3e6bf7796bbdb75076ff6397fd5bd7eb8f65
[babeltrace.git] / src / lib / graph / message / stream-activity.c
1 /*
2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
20 * SOFTWARE.
21 */
22
23 #define BT_LOG_TAG "LIB/MSG-STREAM-ACTIVITY"
24 #include "lib/logging.h"
25
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>
38
39 #include "stream-activity.h"
40
41 static
42 void destroy_stream_activity_message(struct bt_object *obj)
43 {
44 struct bt_message_stream_activity *message = (void *) obj;
45
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);
49
50 if (message->default_cs) {
51 bt_clock_snapshot_recycle(message->default_cs);
52 message->default_cs = NULL;
53 }
54
55 g_free(message);
56 }
57
58 static inline
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)
62 {
63 struct bt_message_stream_activity *message;
64 struct bt_stream_class *stream_class;
65
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);
74 if (!message) {
75 BT_LIB_LOGE_APPEND_CAUSE(
76 "Failed to allocate one stream activity message.");
77 goto error;
78 }
79
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);
84
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) {
89 goto error;
90 }
91 }
92
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);
98
99 return (void *) &message->parent;
100
101 error:
102 return NULL;
103 }
104
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)
108 {
109 return create_stream_activity_message(self_msg_iter, (void *) stream,
110 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING);
111 }
112
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)
116 {
117 return create_stream_activity_message(self_msg_iter, (void *) stream,
118 BT_MESSAGE_TYPE_STREAM_ACTIVITY_END);
119 }
120
121 static inline
122 struct bt_stream *borrow_stream_activity_message_stream(
123 struct bt_message *message)
124 {
125 struct bt_message_stream_activity *stream_act_msg = (void *) message;
126
127 BT_ASSERT(message);
128 return stream_act_msg->stream;
129 }
130
131 struct bt_stream *bt_message_stream_activity_beginning_borrow_stream(
132 struct bt_message *message)
133 {
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);
138 }
139
140 struct bt_stream *bt_message_stream_activity_end_borrow_stream(
141 struct bt_message *message)
142 {
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);
147 }
148
149 const struct bt_stream *bt_message_stream_activity_beginning_borrow_stream_const(
150 const struct bt_message *message)
151 {
152 return bt_message_stream_activity_beginning_borrow_stream(
153 (void *) message);
154 }
155
156 const struct bt_stream *bt_message_stream_activity_end_borrow_stream_const(
157 const struct bt_message *message)
158 {
159 return bt_message_stream_activity_end_borrow_stream((void *) message);
160 }
161
162 static inline
163 void set_stream_activity_message_default_clock_snapshot(
164 struct bt_message *msg, uint64_t value_cycles)
165 {
166 struct bt_message_stream_activity *stream_act_msg = (void *) msg;
167 struct bt_stream_class *sc;
168
169 BT_ASSERT(msg);
170 BT_ASSERT_PRE_HOT(msg, "Message", ": %!+n", msg);
171 sc = stream_act_msg->stream->class;
172 BT_ASSERT(sc);
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,
177 value_cycles);
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);
182 }
183
184 void bt_message_stream_activity_beginning_set_default_clock_snapshot(
185 struct bt_message *msg, uint64_t raw_value)
186 {
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);
191 }
192
193 void bt_message_stream_activity_end_set_default_clock_snapshot(
194 struct bt_message *msg, uint64_t raw_value)
195 {
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);
200 }
201
202 static inline
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)
206 {
207 const struct bt_message_stream_activity *stream_act_msg =
208 (const void *) msg;
209
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;
213 }
214
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)
218 {
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,
223 snapshot);
224 }
225
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)
229 {
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,
233 snapshot);
234 }
235
236 static inline
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)
240 {
241 struct bt_message_stream_activity *stream_act_msg = (void *) msg;
242
243 BT_ASSERT(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",
247 msg,
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));
253 }
254
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)
258 {
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);
263 }
264
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)
268 {
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);
273 }
274
275 static inline
276 const struct bt_clock_class *
277 borrow_stream_activity_message_stream_class_default_clock_class(
278 const struct bt_message *msg)
279 {
280 struct bt_message_stream_activity *stream_act_msg = (void *) msg;
281
282 BT_ASSERT(msg);
283 return stream_act_msg->stream->class->default_clock_class;
284 }
285
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)
289 {
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(
294 msg);
295 }
296
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)
300 {
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(
304 msg);
305 }
This page took 0.034507 seconds and 3 git commands to generate.