Document libbabeltrace2's C API
[babeltrace.git] / src / lib / graph / message / stream.c
CommitLineData
b09a5592
PP
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
b03487ab 24#define BT_LOG_TAG "LIB/MSG-STREAM"
1633ef46 25#include "lib/logging.h"
b09a5592 26
57952005
MJ
27#include "lib/assert-pre.h"
28#include "compat/compiler.h"
7704a0af 29#include <babeltrace2/trace-ir/clock-snapshot.h>
57952005 30#include "lib/trace-ir/stream.h"
71c5da58 31#include <babeltrace2/trace-ir/stream-class.h>
57952005 32#include "lib/trace-ir/stream-class.h"
7704a0af 33#include <babeltrace2/graph/message.h>
57952005 34#include "common/assert.h"
b09a5592
PP
35#include <inttypes.h>
36
57952005
MJ
37#include "stream.h"
38
b09a5592 39static
a1f053a9 40void destroy_stream_message(struct bt_object *obj)
b09a5592 41{
a1f053a9 42 struct bt_message_stream *message = (void *) obj;
b09a5592 43
a1f053a9 44 BT_LIB_LOGD("Destroying stream message: %!+n", message);
9fceecf0
PP
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
b09a5592
PP
53 BT_LIB_LOGD("Putting stream: %!+s", message->stream);
54 BT_OBJECT_PUT_REF_AND_RESET(message->stream);
b09a5592
PP
55 g_free(message);
56}
57
a1f053a9
PP
58static inline
59struct bt_message *create_stream_message(
b09a5592 60 struct bt_self_message_iterator *self_msg_iter,
a1f053a9 61 struct bt_stream *stream, enum bt_message_type type)
b09a5592 62{
a1f053a9 63 struct bt_message_stream *message;
b09a5592
PP
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);
a1f053a9
PP
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);
b09a5592 74 if (!message) {
a8f90e5d
PP
75 BT_LIB_LOGE_APPEND_CAUSE(
76 "Failed to allocate one stream message.");
b09a5592
PP
77 goto error;
78 }
79
a1f053a9
PP
80 bt_message_init(&message->parent, type,
81 destroy_stream_message, NULL);
b09a5592 82 message->stream = stream;
864aa43f 83 bt_object_get_ref_no_null_check(message->stream);
b7cbc799
SM
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
a1f053a9 93 BT_LIB_LOGD("Created stream message object: "
b09a5592
PP
94 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message,
95 stream, stream_class);
96
b7cbc799 97 goto end;
a1f053a9 98
b09a5592 99error:
b7cbc799
SM
100 if (message) {
101 g_free(message);
102 message = NULL;
103 }
104
105end:
106 return &message->parent;
b09a5592
PP
107}
108
a1f053a9
PP
109struct bt_message *bt_message_stream_beginning_create(
110 struct bt_self_message_iterator *self_msg_iter,
6a6975d2 111 const struct bt_stream *stream)
b09a5592 112{
7c7324d3
SM
113 BT_ASSERT_PRE_DEV_NO_ERROR();
114
6a6975d2 115 return create_stream_message(self_msg_iter, (void *) stream,
a1f053a9 116 BT_MESSAGE_TYPE_STREAM_BEGINNING);
b09a5592
PP
117}
118
a1f053a9
PP
119struct bt_message *bt_message_stream_end_create(
120 struct bt_self_message_iterator *self_msg_iter,
6a6975d2 121 const struct bt_stream *stream)
b09a5592 122{
7c7324d3
SM
123 BT_ASSERT_PRE_DEV_NO_ERROR();
124
6a6975d2 125 return create_stream_message(self_msg_iter, (void *) stream,
a1f053a9 126 BT_MESSAGE_TYPE_STREAM_END);
b09a5592
PP
127}
128
a1f053a9
PP
129static inline
130struct bt_stream *borrow_stream_message_stream(struct bt_message *message)
b09a5592 131{
a1f053a9 132 struct bt_message_stream *stream_msg;
b09a5592 133
ec4a3354 134 BT_ASSERT_DBG(message);
a1f053a9
PP
135 stream_msg = (void *) message;
136 return stream_msg->stream;
b09a5592
PP
137}
138
a1f053a9
PP
139struct bt_stream *bt_message_stream_beginning_borrow_stream(
140 struct bt_message *message)
b09a5592 141{
fa6cfec3
PP
142 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
143 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
144 BT_MESSAGE_TYPE_STREAM_BEGINNING);
a1f053a9 145 return borrow_stream_message_stream(message);
b09a5592
PP
146}
147
a1f053a9 148struct bt_stream *bt_message_stream_end_borrow_stream(
b09a5592
PP
149 struct bt_message *message)
150{
fa6cfec3
PP
151 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
152 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
153 BT_MESSAGE_TYPE_STREAM_END);
a1f053a9 154 return borrow_stream_message_stream(message);
b09a5592
PP
155}
156
157const 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}
a1f053a9
PP
163
164const 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}
b7cbc799
SM
170
171static
172void 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);
fa6cfec3 179 BT_ASSERT_PRE_DEV_HOT(msg, "Message", ": %!+n", msg);
b7cbc799
SM
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
192void 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
201void 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
210static enum bt_message_stream_clock_snapshot_state
211bt_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
ec4a3354 217 BT_ASSERT_DBG(msg);
b7cbc799 218 sc = stream_msg->stream->class;
ec4a3354 219 BT_ASSERT_DBG(sc);
fa6cfec3 220 BT_ASSERT_PRE_DEV(sc->default_clock_class,
b7cbc799
SM
221 "Message's stream's class has no default clock class: "
222 "%![msg-]+n, %![sc-]+S", msg, sc);
ec4a3354 223 BT_ASSERT_DBG(stream_msg->default_cs);
b7cbc799
SM
224
225 *snapshot = stream_msg->default_cs;
226
227 return stream_msg->default_cs_state;
228}
229
230enum bt_message_stream_clock_snapshot_state
231bt_message_stream_beginning_borrow_default_clock_snapshot_const(
232 const bt_message *message, const bt_clock_snapshot **snapshot)
233{
fa6cfec3
PP
234 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
235 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
236 BT_MESSAGE_TYPE_STREAM_BEGINNING);
b7cbc799
SM
237 return bt_message_stream_borrow_default_clock_snapshot_const(
238 message, snapshot);
239}
240
241enum bt_message_stream_clock_snapshot_state
242bt_message_stream_end_borrow_default_clock_snapshot_const(
243 const bt_message *message, const bt_clock_snapshot **snapshot)
244{
fa6cfec3
PP
245 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
246 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END);
b7cbc799
SM
247 return bt_message_stream_borrow_default_clock_snapshot_const(
248 message, snapshot);
249}
250
251static inline
252const struct bt_clock_class *
253borrow_stream_message_stream_class_default_clock_class(
254 const struct bt_message *msg)
255{
256 struct bt_message_stream *stream_msg = (void *) msg;
257
ec4a3354 258 BT_ASSERT_DBG(msg);
b7cbc799
SM
259 return stream_msg->stream->class->default_clock_class;
260}
261
262const struct bt_clock_class *
263bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(
264 const struct bt_message *msg)
265{
fa6cfec3
PP
266 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
267 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg,
b7cbc799
SM
268 BT_MESSAGE_TYPE_STREAM_BEGINNING);
269 return borrow_stream_message_stream_class_default_clock_class(msg);
270}
271
272const struct bt_clock_class *
273bt_message_stream_end_borrow_stream_class_default_clock_class_const(
274 const struct bt_message *msg)
275{
fa6cfec3
PP
276 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
277 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_STREAM_END);
b7cbc799
SM
278 return borrow_stream_message_stream_class_default_clock_class(msg);
279}
This page took 0.070299 seconds and 4 git commands to generate.