Document libbabeltrace2's C API
[babeltrace.git] / src / lib / graph / message / stream.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "LIB/MSG-STREAM"
25 #include "lib/logging.h"
26
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"
35 #include <inttypes.h>
36
37 #include "stream.h"
38
39 static
40 void destroy_stream_message(struct bt_object *obj)
41 {
42 struct bt_message_stream *message = (void *) obj;
43
44 BT_LIB_LOGD("Destroying stream message: %!+n", message);
45
46 if (message->default_cs) {
47 BT_LIB_LOGD("Putting default clock snapshot: %!+k",
48 message->default_cs);
49 bt_clock_snapshot_destroy(message->default_cs);
50 message->default_cs = NULL;
51 }
52
53 BT_LIB_LOGD("Putting stream: %!+s", message->stream);
54 BT_OBJECT_PUT_REF_AND_RESET(message->stream);
55 g_free(message);
56 }
57
58 static inline
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)
62 {
63 struct bt_message_stream *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 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);
74 if (!message) {
75 BT_LIB_LOGE_APPEND_CAUSE(
76 "Failed to allocate one stream message.");
77 goto error;
78 }
79
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);
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 BT_LIB_LOGD("Created stream message object: "
94 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message,
95 stream, stream_class);
96
97 goto end;
98
99 error:
100 if (message) {
101 g_free(message);
102 message = NULL;
103 }
104
105 end:
106 return &message->parent;
107 }
108
109 struct bt_message *bt_message_stream_beginning_create(
110 struct bt_self_message_iterator *self_msg_iter,
111 const struct bt_stream *stream)
112 {
113 BT_ASSERT_PRE_DEV_NO_ERROR();
114
115 return create_stream_message(self_msg_iter, (void *) stream,
116 BT_MESSAGE_TYPE_STREAM_BEGINNING);
117 }
118
119 struct bt_message *bt_message_stream_end_create(
120 struct bt_self_message_iterator *self_msg_iter,
121 const struct bt_stream *stream)
122 {
123 BT_ASSERT_PRE_DEV_NO_ERROR();
124
125 return create_stream_message(self_msg_iter, (void *) stream,
126 BT_MESSAGE_TYPE_STREAM_END);
127 }
128
129 static inline
130 struct bt_stream *borrow_stream_message_stream(struct bt_message *message)
131 {
132 struct bt_message_stream *stream_msg;
133
134 BT_ASSERT_DBG(message);
135 stream_msg = (void *) message;
136 return stream_msg->stream;
137 }
138
139 struct bt_stream *bt_message_stream_beginning_borrow_stream(
140 struct bt_message *message)
141 {
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);
146 }
147
148 struct bt_stream *bt_message_stream_end_borrow_stream(
149 struct bt_message *message)
150 {
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);
155 }
156
157 const struct bt_stream *bt_message_stream_beginning_borrow_stream_const(
158 const struct bt_message *message)
159 {
160 return bt_message_stream_beginning_borrow_stream(
161 (void *) message);
162 }
163
164 const struct bt_stream *bt_message_stream_end_borrow_stream_const(
165 const struct bt_message *message)
166 {
167 return bt_message_stream_end_borrow_stream(
168 (void *) message);
169 }
170
171 static
172 void bt_message_stream_set_default_clock_snapshot(
173 struct bt_message *msg, uint64_t raw_value)
174 {
175 struct bt_message_stream *stream_msg = (void *) msg;
176 struct bt_stream_class *sc;
177
178 BT_ASSERT(msg);
179 BT_ASSERT_PRE_DEV_HOT(msg, "Message", ": %!+n", msg);
180 sc = stream_msg->stream->class;
181 BT_ASSERT(sc);
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);
190 }
191
192 void bt_message_stream_beginning_set_default_clock_snapshot(
193 struct bt_message *message, uint64_t raw_value)
194 {
195 BT_ASSERT_PRE_NON_NULL(message, "Message");
196 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_BEGINNING);
197
198 bt_message_stream_set_default_clock_snapshot(message, raw_value);
199 }
200
201 void bt_message_stream_end_set_default_clock_snapshot(
202 struct bt_message *message, uint64_t raw_value)
203 {
204 BT_ASSERT_PRE_NON_NULL(message, "Message");
205 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END);
206
207 return bt_message_stream_set_default_clock_snapshot(message, raw_value);
208 }
209
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)
213 {
214 struct bt_message_stream *stream_msg = (void *) msg;
215 struct bt_stream_class *sc;
216
217 BT_ASSERT_DBG(msg);
218 sc = stream_msg->stream->class;
219 BT_ASSERT_DBG(sc);
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);
224
225 *snapshot = stream_msg->default_cs;
226
227 return stream_msg->default_cs_state;
228 }
229
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)
233 {
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(
238 message, snapshot);
239 }
240
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)
244 {
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(
248 message, snapshot);
249 }
250
251 static inline
252 const struct bt_clock_class *
253 borrow_stream_message_stream_class_default_clock_class(
254 const struct bt_message *msg)
255 {
256 struct bt_message_stream *stream_msg = (void *) msg;
257
258 BT_ASSERT_DBG(msg);
259 return stream_msg->stream->class->default_clock_class;
260 }
261
262 const struct bt_clock_class *
263 bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(
264 const struct bt_message *msg)
265 {
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);
270 }
271
272 const struct bt_clock_class *
273 bt_message_stream_end_borrow_stream_class_default_clock_class_const(
274 const struct bt_message *msg)
275 {
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);
279 }
This page took 0.03377 seconds and 4 git commands to generate.