lib: rename `bt_*_labels_by_value` -> `bt_*_labels_for_value`
[babeltrace.git] / src / lib / graph / message / stream.c
CommitLineData
d6e69534
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
350ad6c1 24#define BT_LOG_TAG "LIB/MSG-STREAM"
c2d9d9cf 25#include "lib/logging.h"
d6e69534 26
578e048b
MJ
27#include "lib/assert-pre.h"
28#include "compat/compiler.h"
3fadfbc0 29#include <babeltrace2/trace-ir/clock-snapshot-const.h>
578e048b 30#include "lib/trace-ir/stream.h"
3fadfbc0 31#include <babeltrace2/trace-ir/stream-class.h>
578e048b 32#include "lib/trace-ir/stream-class.h"
3fadfbc0
MJ
33#include <babeltrace2/graph/message-stream-beginning.h>
34#include <babeltrace2/graph/message-stream-end.h>
35#include <babeltrace2/graph/message-stream-beginning-const.h>
36#include <babeltrace2/graph/message-stream-end-const.h>
578e048b 37#include "common/assert.h"
d6e69534
PP
38#include <inttypes.h>
39
578e048b
MJ
40#include "stream.h"
41
d6e69534 42static
5df26c89 43void destroy_stream_message(struct bt_object *obj)
d6e69534 44{
5df26c89 45 struct bt_message_stream *message = (void *) obj;
d6e69534 46
5df26c89 47 BT_LIB_LOGD("Destroying stream message: %!+n", message);
82cacf47
PP
48
49 if (message->default_cs) {
50 BT_LIB_LOGD("Putting default clock snapshot: %!+k",
51 message->default_cs);
52 bt_clock_snapshot_destroy(message->default_cs);
53 message->default_cs = NULL;
54 }
55
d6e69534
PP
56 BT_LIB_LOGD("Putting stream: %!+s", message->stream);
57 BT_OBJECT_PUT_REF_AND_RESET(message->stream);
d6e69534
PP
58 g_free(message);
59}
60
5df26c89
PP
61static inline
62struct bt_message *create_stream_message(
d6e69534 63 struct bt_self_message_iterator *self_msg_iter,
5df26c89 64 struct bt_stream *stream, enum bt_message_type type)
d6e69534 65{
5df26c89 66 struct bt_message_stream *message;
d6e69534
PP
67 struct bt_stream_class *stream_class;
68
69 BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator");
70 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
71 stream_class = bt_stream_borrow_class(stream);
72 BT_ASSERT(stream_class);
5df26c89
PP
73 BT_LIB_LOGD("Creating stream message object: "
74 "type=%s, %![stream-]+s, %![sc-]+S",
75 bt_message_type_string(type), stream, stream_class);
76 message = g_new0(struct bt_message_stream, 1);
d6e69534 77 if (!message) {
870631a2
PP
78 BT_LIB_LOGE_APPEND_CAUSE(
79 "Failed to allocate one stream message.");
d6e69534
PP
80 goto error;
81 }
82
5df26c89
PP
83 bt_message_init(&message->parent, type,
84 destroy_stream_message, NULL);
d6e69534
PP
85 message->stream = stream;
86 bt_object_get_no_null_check(message->stream);
188edac1
SM
87
88 if (stream_class->default_clock_class) {
89 message->default_cs = bt_clock_snapshot_create(
90 stream_class->default_clock_class);
91 if (!message->default_cs) {
92 goto error;
93 }
94 }
95
5df26c89 96 BT_LIB_LOGD("Created stream message object: "
d6e69534
PP
97 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message,
98 stream, stream_class);
99
188edac1 100 goto end;
5df26c89 101
d6e69534 102error:
188edac1
SM
103 if (message) {
104 g_free(message);
105 message = NULL;
106 }
107
108end:
109 return &message->parent;
d6e69534
PP
110}
111
5df26c89
PP
112struct bt_message *bt_message_stream_beginning_create(
113 struct bt_self_message_iterator *self_msg_iter,
58085ca4 114 const struct bt_stream *stream)
d6e69534 115{
58085ca4 116 return create_stream_message(self_msg_iter, (void *) stream,
5df26c89 117 BT_MESSAGE_TYPE_STREAM_BEGINNING);
d6e69534
PP
118}
119
5df26c89
PP
120struct bt_message *bt_message_stream_end_create(
121 struct bt_self_message_iterator *self_msg_iter,
58085ca4 122 const struct bt_stream *stream)
d6e69534 123{
58085ca4 124 return create_stream_message(self_msg_iter, (void *) stream,
5df26c89 125 BT_MESSAGE_TYPE_STREAM_END);
d6e69534
PP
126}
127
5df26c89
PP
128static inline
129struct bt_stream *borrow_stream_message_stream(struct bt_message *message)
d6e69534 130{
5df26c89 131 struct bt_message_stream *stream_msg;
d6e69534 132
5df26c89
PP
133 BT_ASSERT(message);
134 stream_msg = (void *) message;
135 return stream_msg->stream;
d6e69534
PP
136}
137
5df26c89
PP
138struct bt_stream *bt_message_stream_beginning_borrow_stream(
139 struct bt_message *message)
d6e69534 140{
5df26c89
PP
141 BT_ASSERT_PRE_NON_NULL(message, "Message");
142 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_BEGINNING);
143 return borrow_stream_message_stream(message);
d6e69534
PP
144}
145
5df26c89 146struct bt_stream *bt_message_stream_end_borrow_stream(
d6e69534
PP
147 struct bt_message *message)
148{
d6e69534 149 BT_ASSERT_PRE_NON_NULL(message, "Message");
5df26c89
PP
150 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END);
151 return borrow_stream_message_stream(message);
d6e69534
PP
152}
153
154const struct bt_stream *bt_message_stream_beginning_borrow_stream_const(
155 const struct bt_message *message)
156{
157 return bt_message_stream_beginning_borrow_stream(
158 (void *) message);
159}
5df26c89
PP
160
161const struct bt_stream *bt_message_stream_end_borrow_stream_const(
162 const struct bt_message *message)
163{
164 return bt_message_stream_end_borrow_stream(
165 (void *) message);
166}
188edac1
SM
167
168static
169void bt_message_stream_set_default_clock_snapshot(
170 struct bt_message *msg, uint64_t raw_value)
171{
172 struct bt_message_stream *stream_msg = (void *) msg;
173 struct bt_stream_class *sc;
174
175 BT_ASSERT(msg);
176 BT_ASSERT_PRE_HOT(msg, "Message", ": %!+n", msg);
177 sc = stream_msg->stream->class;
178 BT_ASSERT(sc);
179 BT_ASSERT_PRE(sc->default_clock_class,
180 "Message's stream's class has no default clock class: "
181 "%![msg-]+n, %![sc-]+S", msg, sc);
182 BT_ASSERT(stream_msg->default_cs);
183 bt_clock_snapshot_set_raw_value(stream_msg->default_cs, raw_value);
184 stream_msg->default_cs_state = BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN;
185 BT_LIB_LOGD("Set stream message's default clock snapshot: "
186 "%![msg-]+n, value=%" PRIu64, msg, raw_value);
187}
188
189void bt_message_stream_beginning_set_default_clock_snapshot(
190 struct bt_message *message, uint64_t raw_value)
191{
192 BT_ASSERT_PRE_NON_NULL(message, "Message");
193 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_BEGINNING);
194
195 bt_message_stream_set_default_clock_snapshot(message, raw_value);
196}
197
198void bt_message_stream_end_set_default_clock_snapshot(
199 struct bt_message *message, uint64_t raw_value)
200{
201 BT_ASSERT_PRE_NON_NULL(message, "Message");
202 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END);
203
204 return bt_message_stream_set_default_clock_snapshot(message, raw_value);
205}
206
207static enum bt_message_stream_clock_snapshot_state
208bt_message_stream_borrow_default_clock_snapshot_const(
209 const bt_message *msg, const bt_clock_snapshot **snapshot)
210{
211 struct bt_message_stream *stream_msg = (void *) msg;
212 struct bt_stream_class *sc;
213
214 BT_ASSERT(msg);
215 sc = stream_msg->stream->class;
216 BT_ASSERT(sc);
217 BT_ASSERT_PRE(sc->default_clock_class,
218 "Message's stream's class has no default clock class: "
219 "%![msg-]+n, %![sc-]+S", msg, sc);
220 BT_ASSERT(stream_msg->default_cs);
221
222 *snapshot = stream_msg->default_cs;
223
224 return stream_msg->default_cs_state;
225}
226
227enum bt_message_stream_clock_snapshot_state
228bt_message_stream_beginning_borrow_default_clock_snapshot_const(
229 const bt_message *message, const bt_clock_snapshot **snapshot)
230{
231 BT_ASSERT_PRE_NON_NULL(message, "Message");
232 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_BEGINNING);
233
234 return bt_message_stream_borrow_default_clock_snapshot_const(
235 message, snapshot);
236}
237
238enum bt_message_stream_clock_snapshot_state
239bt_message_stream_end_borrow_default_clock_snapshot_const(
240 const bt_message *message, const bt_clock_snapshot **snapshot)
241{
242 BT_ASSERT_PRE_NON_NULL(message, "Message");
243 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END);
244
245 return bt_message_stream_borrow_default_clock_snapshot_const(
246 message, snapshot);
247}
248
249static inline
250const struct bt_clock_class *
251borrow_stream_message_stream_class_default_clock_class(
252 const struct bt_message *msg)
253{
254 struct bt_message_stream *stream_msg = (void *) msg;
255
256 BT_ASSERT(msg);
257 return stream_msg->stream->class->default_clock_class;
258}
259
260const struct bt_clock_class *
261bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(
262 const struct bt_message *msg)
263{
264 BT_ASSERT_PRE_NON_NULL(msg, "Message");
265 BT_ASSERT_PRE_MSG_IS_TYPE(msg,
266 BT_MESSAGE_TYPE_STREAM_BEGINNING);
267 return borrow_stream_message_stream_class_default_clock_class(msg);
268}
269
270const struct bt_clock_class *
271bt_message_stream_end_borrow_stream_class_default_clock_class_const(
272 const struct bt_message *msg)
273{
274 BT_ASSERT_PRE_NON_NULL(msg, "Message");
275 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_STREAM_END);
276 return borrow_stream_message_stream_class_default_clock_class(msg);
277}
This page took 0.052113 seconds and 4 git commands to generate.