Visibility hidden by default
[babeltrace.git] / src / lib / graph / message / stream.c
CommitLineData
d6e69534 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
d6e69534
PP
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
d6e69534
PP
6 */
7
350ad6c1 8#define BT_LOG_TAG "LIB/MSG-STREAM"
c2d9d9cf 9#include "lib/logging.h"
d6e69534 10
d98421f2 11#include "lib/assert-cond.h"
578e048b 12#include "compat/compiler.h"
43c59509 13#include <babeltrace2/trace-ir/clock-snapshot.h>
578e048b 14#include "lib/trace-ir/stream.h"
3fadfbc0 15#include <babeltrace2/trace-ir/stream-class.h>
578e048b 16#include "lib/trace-ir/stream-class.h"
43c59509 17#include <babeltrace2/graph/message.h>
578e048b 18#include "common/assert.h"
d6e69534
PP
19#include <inttypes.h>
20
578e048b
MJ
21#include "stream.h"
22
1778c2a4
PP
23#define BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(_msg) \
24 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg), \
25 "stream-beginning", BT_MESSAGE_TYPE_STREAM_BEGINNING)
26
27#define BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(_msg) \
28 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg), \
29 "stream-end", BT_MESSAGE_TYPE_STREAM_END)
30
d6e69534 31static
5df26c89 32void destroy_stream_message(struct bt_object *obj)
d6e69534 33{
5df26c89 34 struct bt_message_stream *message = (void *) obj;
d6e69534 35
5df26c89 36 BT_LIB_LOGD("Destroying stream message: %!+n", message);
82cacf47
PP
37
38 if (message->default_cs) {
39 BT_LIB_LOGD("Putting default clock snapshot: %!+k",
40 message->default_cs);
41 bt_clock_snapshot_destroy(message->default_cs);
42 message->default_cs = NULL;
43 }
44
d6e69534
PP
45 BT_LIB_LOGD("Putting stream: %!+s", message->stream);
46 BT_OBJECT_PUT_REF_AND_RESET(message->stream);
d6e69534
PP
47 g_free(message);
48}
49
5df26c89
PP
50static inline
51struct bt_message *create_stream_message(
d6e69534 52 struct bt_self_message_iterator *self_msg_iter,
1778c2a4
PP
53 struct bt_stream *stream, enum bt_message_type type,
54 const char *api_func)
d6e69534 55{
5df26c89 56 struct bt_message_stream *message;
d6e69534
PP
57 struct bt_stream_class *stream_class;
58
1778c2a4
PP
59 BT_ASSERT_PRE_MSG_ITER_NON_NULL_FROM_FUNC(api_func, self_msg_iter);
60 BT_ASSERT_PRE_STREAM_NON_NULL_FROM_FUNC(api_func, stream);
d6e69534
PP
61 stream_class = bt_stream_borrow_class(stream);
62 BT_ASSERT(stream_class);
5df26c89
PP
63 BT_LIB_LOGD("Creating stream message object: "
64 "type=%s, %![stream-]+s, %![sc-]+S",
1e123ed6 65 bt_common_message_type_string(type), stream, stream_class);
5df26c89 66 message = g_new0(struct bt_message_stream, 1);
d6e69534 67 if (!message) {
870631a2
PP
68 BT_LIB_LOGE_APPEND_CAUSE(
69 "Failed to allocate one stream message.");
d6e69534
PP
70 goto error;
71 }
72
5df26c89
PP
73 bt_message_init(&message->parent, type,
74 destroy_stream_message, NULL);
d6e69534 75 message->stream = stream;
6871026b 76 bt_object_get_ref_no_null_check(message->stream);
188edac1
SM
77
78 if (stream_class->default_clock_class) {
79 message->default_cs = bt_clock_snapshot_create(
80 stream_class->default_clock_class);
81 if (!message->default_cs) {
82 goto error;
83 }
84 }
85
5df26c89 86 BT_LIB_LOGD("Created stream message object: "
d6e69534
PP
87 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message,
88 stream, stream_class);
89
188edac1 90 goto end;
5df26c89 91
d6e69534 92error:
188edac1
SM
93 if (message) {
94 g_free(message);
95 message = NULL;
96 }
97
98end:
99 return &message->parent;
d6e69534
PP
100}
101
1353b066 102BT_EXPORT
5df26c89
PP
103struct bt_message *bt_message_stream_beginning_create(
104 struct bt_self_message_iterator *self_msg_iter,
58085ca4 105 const struct bt_stream *stream)
d6e69534 106{
17f3083a
SM
107 BT_ASSERT_PRE_DEV_NO_ERROR();
108
58085ca4 109 return create_stream_message(self_msg_iter, (void *) stream,
1778c2a4 110 BT_MESSAGE_TYPE_STREAM_BEGINNING, __func__);
d6e69534
PP
111}
112
1353b066 113BT_EXPORT
5df26c89
PP
114struct bt_message *bt_message_stream_end_create(
115 struct bt_self_message_iterator *self_msg_iter,
58085ca4 116 const struct bt_stream *stream)
d6e69534 117{
17f3083a
SM
118 BT_ASSERT_PRE_DEV_NO_ERROR();
119
58085ca4 120 return create_stream_message(self_msg_iter, (void *) stream,
1778c2a4 121 BT_MESSAGE_TYPE_STREAM_END, __func__);
d6e69534
PP
122}
123
5df26c89
PP
124static inline
125struct bt_stream *borrow_stream_message_stream(struct bt_message *message)
d6e69534 126{
5df26c89 127 struct bt_message_stream *stream_msg;
d6e69534 128
98b15851 129 BT_ASSERT_DBG(message);
5df26c89
PP
130 stream_msg = (void *) message;
131 return stream_msg->stream;
d6e69534
PP
132}
133
1353b066 134BT_EXPORT
5df26c89
PP
135struct bt_stream *bt_message_stream_beginning_borrow_stream(
136 struct bt_message *message)
d6e69534 137{
d5b13b9b 138 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
1778c2a4 139 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(message);
5df26c89 140 return borrow_stream_message_stream(message);
d6e69534
PP
141}
142
1353b066 143BT_EXPORT
5df26c89 144struct bt_stream *bt_message_stream_end_borrow_stream(
d6e69534
PP
145 struct bt_message *message)
146{
d5b13b9b 147 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
1778c2a4 148 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(message);
5df26c89 149 return borrow_stream_message_stream(message);
d6e69534
PP
150}
151
1353b066 152BT_EXPORT
d6e69534
PP
153const struct bt_stream *bt_message_stream_beginning_borrow_stream_const(
154 const struct bt_message *message)
155{
156 return bt_message_stream_beginning_borrow_stream(
157 (void *) message);
158}
5df26c89 159
1353b066 160BT_EXPORT
5df26c89
PP
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
1778c2a4
PP
169void set_stream_default_clock_snapshot(
170 struct bt_message *msg, uint64_t raw_value,
171 const char *api_func)
188edac1
SM
172{
173 struct bt_message_stream *stream_msg = (void *) msg;
174 struct bt_stream_class *sc;
175
176 BT_ASSERT(msg);
1778c2a4
PP
177 BT_ASSERT_PRE_DEV_HOT_FROM_FUNC(api_func, "message", msg,
178 "Message", ": %!+n", msg);
188edac1
SM
179 sc = stream_msg->stream->class;
180 BT_ASSERT(sc);
1778c2a4 181 BT_ASSERT_PRE_MSG_SC_DEF_CLK_CLS_FROM_FUNC(api_func, msg, sc);
188edac1
SM
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
1353b066 189BT_EXPORT
188edac1
SM
190void bt_message_stream_beginning_set_default_clock_snapshot(
191 struct bt_message *message, uint64_t raw_value)
192{
d5b13b9b 193 BT_ASSERT_PRE_MSG_NON_NULL(message);
1778c2a4
PP
194 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(message);
195 set_stream_default_clock_snapshot(message, raw_value, __func__);
188edac1
SM
196}
197
1353b066 198BT_EXPORT
188edac1
SM
199void bt_message_stream_end_set_default_clock_snapshot(
200 struct bt_message *message, uint64_t raw_value)
201{
d5b13b9b 202 BT_ASSERT_PRE_MSG_NON_NULL(message);
1778c2a4
PP
203 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(message);
204 return set_stream_default_clock_snapshot(message, raw_value, __func__);
188edac1
SM
205}
206
207static enum bt_message_stream_clock_snapshot_state
1778c2a4
PP
208borrow_stream_message_default_clock_snapshot_const(
209 const bt_message *msg, const bt_clock_snapshot **snapshot,
210 const char *api_func)
188edac1
SM
211{
212 struct bt_message_stream *stream_msg = (void *) msg;
213 struct bt_stream_class *sc;
214
98b15851 215 BT_ASSERT_DBG(msg);
188edac1 216 sc = stream_msg->stream->class;
98b15851 217 BT_ASSERT_DBG(sc);
1778c2a4 218 BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS_FROM_FUNC(api_func, msg, sc);
98b15851 219 BT_ASSERT_DBG(stream_msg->default_cs);
188edac1
SM
220
221 *snapshot = stream_msg->default_cs;
222
223 return stream_msg->default_cs_state;
224}
225
1353b066 226BT_EXPORT
188edac1
SM
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{
d5b13b9b 231 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
1778c2a4
PP
232 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(message);
233 return borrow_stream_message_default_clock_snapshot_const(
234 message, snapshot, __func__);
188edac1
SM
235}
236
1353b066 237BT_EXPORT
188edac1
SM
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{
d5b13b9b 242 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
1778c2a4
PP
243 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(message);
244 return borrow_stream_message_default_clock_snapshot_const(
245 message, snapshot, __func__);
188edac1
SM
246}
247
248static inline
249const struct bt_clock_class *
250borrow_stream_message_stream_class_default_clock_class(
251 const struct bt_message *msg)
252{
253 struct bt_message_stream *stream_msg = (void *) msg;
254
98b15851 255 BT_ASSERT_DBG(msg);
188edac1
SM
256 return stream_msg->stream->class->default_clock_class;
257}
258
1353b066 259BT_EXPORT
188edac1
SM
260const struct bt_clock_class *
261bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(
262 const struct bt_message *msg)
263{
d5b13b9b 264 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
1778c2a4 265 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_BEGINNING(msg);
188edac1
SM
266 return borrow_stream_message_stream_class_default_clock_class(msg);
267}
268
1353b066 269BT_EXPORT
188edac1
SM
270const struct bt_clock_class *
271bt_message_stream_end_borrow_stream_class_default_clock_class_const(
272 const struct bt_message *msg)
273{
d5b13b9b 274 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
1778c2a4 275 BT_ASSERT_PRE_DEV_MSG_IS_STREAM_END(msg);
188edac1
SM
276 return borrow_stream_message_stream_class_default_clock_class(msg);
277}
This page took 0.085555 seconds and 4 git commands to generate.