f92fc3d068e1b8da5cd91517816f67bcf98a1165
[babeltrace.git] / src / lib / graph / message / stream.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #define BT_LOG_TAG "LIB/MSG-STREAM"
9 #include "lib/logging.h"
10
11 #include "lib/assert-cond.h"
12 #include "compat/compiler.h"
13 #include <babeltrace2/trace-ir/clock-snapshot.h>
14 #include "lib/trace-ir/stream.h"
15 #include <babeltrace2/trace-ir/stream-class.h>
16 #include "lib/trace-ir/stream-class.h"
17 #include <babeltrace2/graph/message.h>
18 #include "common/assert.h"
19 #include <inttypes.h>
20
21 #include "stream.h"
22
23 static
24 void destroy_stream_message(struct bt_object *obj)
25 {
26 struct bt_message_stream *message = (void *) obj;
27
28 BT_LIB_LOGD("Destroying stream message: %!+n", message);
29
30 if (message->default_cs) {
31 BT_LIB_LOGD("Putting default clock snapshot: %!+k",
32 message->default_cs);
33 bt_clock_snapshot_destroy(message->default_cs);
34 message->default_cs = NULL;
35 }
36
37 BT_LIB_LOGD("Putting stream: %!+s", message->stream);
38 BT_OBJECT_PUT_REF_AND_RESET(message->stream);
39 g_free(message);
40 }
41
42 static inline
43 struct bt_message *create_stream_message(
44 struct bt_self_message_iterator *self_msg_iter,
45 struct bt_stream *stream, enum bt_message_type type)
46 {
47 struct bt_message_stream *message;
48 struct bt_stream_class *stream_class;
49
50 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter);
51 BT_ASSERT_PRE_STREAM_NON_NULL(stream);
52 stream_class = bt_stream_borrow_class(stream);
53 BT_ASSERT(stream_class);
54 BT_LIB_LOGD("Creating stream message object: "
55 "type=%s, %![stream-]+s, %![sc-]+S",
56 bt_message_type_string(type), stream, stream_class);
57 message = g_new0(struct bt_message_stream, 1);
58 if (!message) {
59 BT_LIB_LOGE_APPEND_CAUSE(
60 "Failed to allocate one stream message.");
61 goto error;
62 }
63
64 bt_message_init(&message->parent, type,
65 destroy_stream_message, NULL);
66 message->stream = stream;
67 bt_object_get_ref_no_null_check(message->stream);
68
69 if (stream_class->default_clock_class) {
70 message->default_cs = bt_clock_snapshot_create(
71 stream_class->default_clock_class);
72 if (!message->default_cs) {
73 goto error;
74 }
75 }
76
77 BT_LIB_LOGD("Created stream message object: "
78 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message,
79 stream, stream_class);
80
81 goto end;
82
83 error:
84 if (message) {
85 g_free(message);
86 message = NULL;
87 }
88
89 end:
90 return &message->parent;
91 }
92
93 struct bt_message *bt_message_stream_beginning_create(
94 struct bt_self_message_iterator *self_msg_iter,
95 const struct bt_stream *stream)
96 {
97 BT_ASSERT_PRE_DEV_NO_ERROR();
98
99 return create_stream_message(self_msg_iter, (void *) stream,
100 BT_MESSAGE_TYPE_STREAM_BEGINNING);
101 }
102
103 struct bt_message *bt_message_stream_end_create(
104 struct bt_self_message_iterator *self_msg_iter,
105 const struct bt_stream *stream)
106 {
107 BT_ASSERT_PRE_DEV_NO_ERROR();
108
109 return create_stream_message(self_msg_iter, (void *) stream,
110 BT_MESSAGE_TYPE_STREAM_END);
111 }
112
113 static inline
114 struct bt_stream *borrow_stream_message_stream(struct bt_message *message)
115 {
116 struct bt_message_stream *stream_msg;
117
118 BT_ASSERT_DBG(message);
119 stream_msg = (void *) message;
120 return stream_msg->stream;
121 }
122
123 struct bt_stream *bt_message_stream_beginning_borrow_stream(
124 struct bt_message *message)
125 {
126 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
127 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(message,
128 BT_MESSAGE_TYPE_STREAM_BEGINNING);
129 return borrow_stream_message_stream(message);
130 }
131
132 struct bt_stream *bt_message_stream_end_borrow_stream(
133 struct bt_message *message)
134 {
135 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
136 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(message,
137 BT_MESSAGE_TYPE_STREAM_END);
138 return borrow_stream_message_stream(message);
139 }
140
141 const struct bt_stream *bt_message_stream_beginning_borrow_stream_const(
142 const struct bt_message *message)
143 {
144 return bt_message_stream_beginning_borrow_stream(
145 (void *) message);
146 }
147
148 const struct bt_stream *bt_message_stream_end_borrow_stream_const(
149 const struct bt_message *message)
150 {
151 return bt_message_stream_end_borrow_stream(
152 (void *) message);
153 }
154
155 static
156 void bt_message_stream_set_default_clock_snapshot(
157 struct bt_message *msg, uint64_t raw_value)
158 {
159 struct bt_message_stream *stream_msg = (void *) msg;
160 struct bt_stream_class *sc;
161
162 BT_ASSERT(msg);
163 BT_ASSERT_PRE_DEV_HOT(msg, "Message", ": %!+n", msg);
164 sc = stream_msg->stream->class;
165 BT_ASSERT(sc);
166 BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS(msg, sc);
167 BT_ASSERT(stream_msg->default_cs);
168 bt_clock_snapshot_set_raw_value(stream_msg->default_cs, raw_value);
169 stream_msg->default_cs_state = BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN;
170 BT_LIB_LOGD("Set stream message's default clock snapshot: "
171 "%![msg-]+n, value=%" PRIu64, msg, raw_value);
172 }
173
174 void bt_message_stream_beginning_set_default_clock_snapshot(
175 struct bt_message *message, uint64_t raw_value)
176 {
177 BT_ASSERT_PRE_MSG_NON_NULL(message);
178 BT_ASSERT_PRE_MSG_HAS_TYPE(message, BT_MESSAGE_TYPE_STREAM_BEGINNING);
179
180 bt_message_stream_set_default_clock_snapshot(message, raw_value);
181 }
182
183 void bt_message_stream_end_set_default_clock_snapshot(
184 struct bt_message *message, uint64_t raw_value)
185 {
186 BT_ASSERT_PRE_MSG_NON_NULL(message);
187 BT_ASSERT_PRE_MSG_HAS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END);
188
189 return bt_message_stream_set_default_clock_snapshot(message, raw_value);
190 }
191
192 static enum bt_message_stream_clock_snapshot_state
193 bt_message_stream_borrow_default_clock_snapshot_const(
194 const bt_message *msg, const bt_clock_snapshot **snapshot)
195 {
196 struct bt_message_stream *stream_msg = (void *) msg;
197 struct bt_stream_class *sc;
198
199 BT_ASSERT_DBG(msg);
200 sc = stream_msg->stream->class;
201 BT_ASSERT_DBG(sc);
202 BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS(msg, sc);
203 BT_ASSERT_DBG(stream_msg->default_cs);
204
205 *snapshot = stream_msg->default_cs;
206
207 return stream_msg->default_cs_state;
208 }
209
210 enum bt_message_stream_clock_snapshot_state
211 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
212 const bt_message *message, const bt_clock_snapshot **snapshot)
213 {
214 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
215 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(message,
216 BT_MESSAGE_TYPE_STREAM_BEGINNING);
217 return bt_message_stream_borrow_default_clock_snapshot_const(
218 message, snapshot);
219 }
220
221 enum bt_message_stream_clock_snapshot_state
222 bt_message_stream_end_borrow_default_clock_snapshot_const(
223 const bt_message *message, const bt_clock_snapshot **snapshot)
224 {
225 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
226 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END);
227 return bt_message_stream_borrow_default_clock_snapshot_const(
228 message, snapshot);
229 }
230
231 static inline
232 const struct bt_clock_class *
233 borrow_stream_message_stream_class_default_clock_class(
234 const struct bt_message *msg)
235 {
236 struct bt_message_stream *stream_msg = (void *) msg;
237
238 BT_ASSERT_DBG(msg);
239 return stream_msg->stream->class->default_clock_class;
240 }
241
242 const struct bt_clock_class *
243 bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(
244 const struct bt_message *msg)
245 {
246 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
247 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg,
248 BT_MESSAGE_TYPE_STREAM_BEGINNING);
249 return borrow_stream_message_stream_class_default_clock_class(msg);
250 }
251
252 const struct bt_clock_class *
253 bt_message_stream_end_borrow_stream_class_default_clock_class_const(
254 const struct bt_message *msg)
255 {
256 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
257 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_STREAM_END);
258 return borrow_stream_message_stream_class_default_clock_class(msg);
259 }
This page took 0.034043 seconds and 3 git commands to generate.