Clock snapshot API: use status
[babeltrace.git] / 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 "MSG-STREAM"
25 #include <babeltrace/lib-logging-internal.h>
26
27 #include <babeltrace/assert-pre-internal.h>
28 #include <babeltrace/compiler-internal.h>
29 #include <babeltrace/trace-ir/clock-snapshot-const.h>
30 #include <babeltrace/trace-ir/stream-internal.h>
31 #include <babeltrace/trace-ir/stream-class.h>
32 #include <babeltrace/trace-ir/stream-class-internal.h>
33 #include <babeltrace/graph/message-stream.h>
34 #include <babeltrace/graph/message-stream-const.h>
35 #include <babeltrace/graph/message-stream-internal.h>
36 #include <babeltrace/assert-internal.h>
37 #include <inttypes.h>
38
39 static
40 void bt_message_stream_end_destroy(struct bt_object *obj)
41 {
42 struct bt_message_stream_end *message =
43 (struct bt_message_stream_end *) obj;
44
45 BT_LIB_LOGD("Destroying stream end message: %!+n",
46 message);
47 BT_LIB_LOGD("Putting stream: %!+s", message->stream);
48 BT_OBJECT_PUT_REF_AND_RESET(message->stream);
49
50 if (message->default_cs) {
51 bt_clock_snapshot_recycle(message->default_cs);
52 message->default_cs = NULL;
53 }
54
55 g_free(message);
56 }
57
58 struct bt_message *bt_message_stream_end_create(
59 struct bt_self_message_iterator *self_msg_iter,
60 struct bt_stream *stream)
61 {
62 struct bt_message_stream_end *message;
63 struct bt_stream_class *stream_class;
64
65 BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator");
66 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
67 stream_class = bt_stream_borrow_class(stream);
68 BT_ASSERT(stream_class);
69 BT_LIB_LOGD("Creating stream end message object: "
70 "%![stream-]+s, %![sc-]+S", stream, stream_class);
71 message = g_new0(struct bt_message_stream_end, 1);
72 if (!message) {
73 BT_LOGE_STR("Failed to allocate one stream end message.");
74 goto error;
75 }
76
77 bt_message_init(&message->parent,
78 BT_MESSAGE_TYPE_STREAM_END,
79 bt_message_stream_end_destroy, NULL);
80 message->stream = stream;
81 bt_object_get_no_null_check(message->stream);
82 BT_LIB_LOGD("Created stream end message object: "
83 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message,
84 stream, stream_class);
85
86 return (void *) &message->parent;
87 error:
88 return NULL;
89 }
90
91 struct bt_stream *bt_message_stream_end_borrow_stream(
92 struct bt_message *message)
93 {
94 struct bt_message_stream_end *stream_end;
95
96 BT_ASSERT_PRE_NON_NULL(message, "Message");
97 BT_ASSERT_PRE_MSG_IS_TYPE(message,
98 BT_MESSAGE_TYPE_STREAM_END);
99 stream_end = (void *) message;
100 return stream_end->stream;
101 }
102
103 const struct bt_stream *bt_message_stream_end_borrow_stream_const(
104 const struct bt_message *message)
105 {
106 return bt_message_stream_end_borrow_stream(
107 (void *) message);
108 }
109
110 void bt_message_stream_end_set_default_clock_snapshot(
111 struct bt_message *msg, uint64_t value_cycles)
112 {
113 struct bt_message_stream_end *se_msg = (void *) msg;
114
115 BT_ASSERT_PRE_NON_NULL(msg, "Message");
116 BT_ASSERT_PRE_HOT(msg, "Message", ": %!+n", msg);
117 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_STREAM_END);
118 BT_ASSERT_PRE(se_msg->stream->class->default_clock_class,
119 "Message's stream class has no default clock class: "
120 "%![msg-]+n, %![sc-]+S", msg, se_msg->stream->class);
121
122 /* TODO: have the object already created */
123 se_msg->default_cs = bt_clock_snapshot_create(
124 se_msg->stream->class->default_clock_class);
125 BT_ASSERT(se_msg->default_cs);
126 bt_clock_snapshot_set_value_inline(se_msg->default_cs, value_cycles);
127 BT_LIB_LOGV("Set message's default clock snapshot: %![msg-]+n, "
128 "value=%" PRIu64, value_cycles);
129 }
130
131 enum bt_clock_snapshot_state
132 bt_message_stream_end_borrow_default_clock_snapshot_const(
133 const bt_message *msg, const bt_clock_snapshot **snapshot)
134 {
135 struct bt_message_stream_end *stream_end = (void *) msg;
136
137 BT_ASSERT_PRE_NON_NULL(msg, "Message");
138 BT_ASSERT_PRE_NON_NULL(snapshot, "Clock snapshot (output)");
139 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_STREAM_END);
140 *snapshot = stream_end->default_cs;
141 return BT_CLOCK_SNAPSHOT_STATE_KNOWN;
142 }
143
144 static
145 void bt_message_stream_beginning_destroy(struct bt_object *obj)
146 {
147 struct bt_message_stream_beginning *message =
148 (struct bt_message_stream_beginning *) obj;
149
150 BT_LIB_LOGD("Destroying stream beginning message: %!+n",
151 message);
152 BT_LIB_LOGD("Putting stream: %!+s", message->stream);
153 BT_OBJECT_PUT_REF_AND_RESET(message->stream);
154
155 if (message->default_cs) {
156 bt_clock_snapshot_recycle(message->default_cs);
157 message->default_cs = NULL;
158 }
159
160 g_free(message);
161 }
162
163 struct bt_message *bt_message_stream_beginning_create(
164 struct bt_self_message_iterator *self_msg_iter,
165 struct bt_stream *stream)
166 {
167 struct bt_message_stream_beginning *message;
168 struct bt_stream_class *stream_class;
169
170 BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator");
171 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
172 stream_class = bt_stream_borrow_class(stream);
173 BT_ASSERT(stream_class);
174 BT_LIB_LOGD("Creating stream beginning message object: "
175 "%![stream-]+s, %![sc-]+S", stream, stream_class);
176 message = g_new0(struct bt_message_stream_beginning, 1);
177 if (!message) {
178 BT_LOGE_STR("Failed to allocate one stream beginning message.");
179 goto error;
180 }
181
182 bt_message_init(&message->parent,
183 BT_MESSAGE_TYPE_STREAM_BEGINNING,
184 bt_message_stream_beginning_destroy, NULL);
185 message->stream = stream;
186 bt_object_get_no_null_check(message->stream);
187 BT_LIB_LOGD("Created stream beginning message object: "
188 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message,
189 stream, stream_class);
190 return (void *) &message->parent;
191 error:
192 return NULL;
193 }
194
195 struct bt_stream *bt_message_stream_beginning_borrow_stream(
196 struct bt_message *message)
197 {
198 struct bt_message_stream_beginning *stream_begin;
199
200 BT_ASSERT_PRE_NON_NULL(message, "Message");
201 BT_ASSERT_PRE_MSG_IS_TYPE(message,
202 BT_MESSAGE_TYPE_STREAM_BEGINNING);
203 stream_begin = (void *) message;
204 return stream_begin->stream;
205 }
206
207 const struct bt_stream *bt_message_stream_beginning_borrow_stream_const(
208 const struct bt_message *message)
209 {
210 return bt_message_stream_beginning_borrow_stream(
211 (void *) message);
212 }
213
214 void bt_message_stream_beginning_set_default_clock_snapshot(
215 struct bt_message *msg,
216 uint64_t value_cycles)
217 {
218 struct bt_message_stream_beginning *sb_msg = (void *) msg;
219
220 BT_ASSERT_PRE_NON_NULL(msg, "Message");
221 BT_ASSERT_PRE_HOT(msg, "Message", ": %!+n", msg);
222 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_STREAM_BEGINNING);
223 BT_ASSERT_PRE(sb_msg->stream->class->default_clock_class,
224 "Message's stream class has no default clock class: "
225 "%![msg-]+n, %![sc-]+S", msg, sb_msg->stream->class);
226
227 /* TODO: have the object already created */
228 sb_msg->default_cs = bt_clock_snapshot_create(
229 sb_msg->stream->class->default_clock_class);
230 BT_ASSERT(sb_msg->default_cs);
231 bt_clock_snapshot_set_value_inline(sb_msg->default_cs, value_cycles);
232 BT_LIB_LOGV("Set message's default clock snapshot: %![msg-]+n, "
233 "value=%" PRIu64, value_cycles);
234 }
235
236 enum bt_clock_snapshot_state
237 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
238 const bt_message *msg, const bt_clock_snapshot **snapshot)
239 {
240 struct bt_message_stream_beginning *stream_beginning = (void *) msg;
241
242 BT_ASSERT_PRE_NON_NULL(msg, "Message");
243 BT_ASSERT_PRE_NON_NULL(snapshot, "Clock snapshot (output)");
244 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_STREAM_END);
245 *snapshot = stream_beginning->default_cs;
246 return BT_CLOCK_SNAPSHOT_STATE_KNOWN;
247 }
This page took 0.033591 seconds and 4 git commands to generate.