lib: add stream activity beginning/end messages
[babeltrace.git] / 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 "MSG-STREAM-ACTIVITY"
24 #include <babeltrace/lib-logging-internal.h>
25
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>
39
40 static
41 void destroy_stream_activity_message(struct bt_object *obj)
42 {
43 struct bt_message_stream_activity *message = (void *) obj;
44
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);
48
49 if (message->default_cs) {
50 bt_clock_snapshot_recycle(message->default_cs);
51 }
52
53 g_free(message);
54 }
55
56 static inline
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)
60 {
61 struct bt_message_stream_activity *message;
62 struct bt_stream_class *stream_class;
63
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);
72 if (!message) {
73 BT_LOGE_STR("Failed to allocate one stream activity message.");
74 goto error;
75 }
76
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);
81
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) {
86 goto error;
87 }
88 }
89
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);
95
96 return (void *) &message->parent;
97
98 error:
99 return NULL;
100 }
101
102 struct bt_message *bt_message_stream_activity_beginning_create(
103 struct bt_self_message_iterator *self_msg_iter,
104 struct bt_stream *stream)
105 {
106 return create_stream_activity_message(self_msg_iter, stream,
107 BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING);
108 }
109
110 struct bt_message *bt_message_stream_activity_end_create(
111 struct bt_self_message_iterator *self_msg_iter,
112 struct bt_stream *stream)
113 {
114 return create_stream_activity_message(self_msg_iter, stream,
115 BT_MESSAGE_TYPE_STREAM_ACTIVITY_END);
116 }
117
118 static inline
119 struct bt_stream *borrow_stream_activity_message_stream(
120 struct bt_message *message)
121 {
122 struct bt_message_stream_activity *stream_act_msg = (void *) message;
123
124 BT_ASSERT(message);
125 return stream_act_msg->stream;
126 }
127
128 struct bt_stream *bt_message_stream_activity_beginning_borrow_stream(
129 struct bt_message *message)
130 {
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);
135 }
136
137 struct bt_stream *bt_message_stream_activity_end_borrow_stream(
138 struct bt_message *message)
139 {
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);
144 }
145
146 const struct bt_stream *bt_message_stream_activity_beginning_borrow_stream_const(
147 const struct bt_message *message)
148 {
149 return bt_message_stream_activity_beginning_borrow_stream(
150 (void *) message);
151 }
152
153 const struct bt_stream *bt_message_stream_activity_end_borrow_stream_const(
154 const struct bt_message *message)
155 {
156 return bt_message_stream_activity_end_borrow_stream((void *) message);
157 }
158
159 static inline
160 void set_stream_activity_message_default_clock_snapshot(
161 struct bt_message *msg, uint64_t value_cycles)
162 {
163 struct bt_message_stream_activity *stream_act_msg = (void *) msg;
164 struct bt_stream_class *sc;
165
166 BT_ASSERT(msg);
167 BT_ASSERT_PRE_HOT(msg, "Message", ": %!+n", msg);
168 sc = stream_act_msg->stream->class;
169 BT_ASSERT(sc);
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,
174 value_cycles);
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);
179 }
180
181 void bt_message_stream_activity_beginning_set_default_clock_snapshot(
182 struct bt_message *msg, uint64_t raw_value)
183 {
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);
188 }
189
190 void bt_message_stream_activity_end_set_default_clock_snapshot(
191 struct bt_message *msg, uint64_t raw_value)
192 {
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);
197 }
198
199 static inline
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)
203 {
204 const struct bt_message_stream_activity *stream_act_msg =
205 (const void *) msg;
206
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;
210 }
211
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)
215 {
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,
220 snapshot);
221 }
222
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)
226 {
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,
230 snapshot);
231 }
232
233 static inline
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)
237 {
238 struct bt_message_stream_activity *stream_act_msg = (void *) msg;
239
240 BT_ASSERT(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",
244 msg,
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));
250 }
251
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)
255 {
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);
260 }
261
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)
265 {
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);
270 }
This page took 0.034463 seconds and 4 git commands to generate.