Logging: standardize logging tags
[babeltrace.git] / src / lib / graph / message / discarded-items.c
1 /*
2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #define BT_LOG_TAG "LIB/MSG-DISCARDED-ITEMS"
24 #include "lib/lib-logging.h"
25
26 #include "lib/assert-pre.h"
27 #include "lib/object.h"
28 #include "compat/compiler.h"
29 #include <babeltrace2/trace-ir/clock-class.h>
30 #include "lib/trace-ir/clock-snapshot.h"
31 #include "lib/trace-ir/stream-class.h"
32 #include "lib/trace-ir/stream.h"
33 #include "lib/property.h"
34 #include "lib/graph/message/message.h"
35 #include <babeltrace2/graph/message-discarded-events.h>
36 #include <babeltrace2/graph/message-discarded-events-const.h>
37 #include <babeltrace2/graph/message-discarded-packets.h>
38 #include <babeltrace2/graph/message-discarded-packets-const.h>
39
40 #include "discarded-items.h"
41
42 static
43 void destroy_discarded_items_message(struct bt_object *obj)
44 {
45 struct bt_message_discarded_items *message = (void *) obj;
46
47 BT_LIB_LOGD("Destroying discarded items message: %!+n", message);
48 BT_LIB_LOGD("Putting stream: %!+s", message->stream);
49 BT_OBJECT_PUT_REF_AND_RESET(message->stream);
50
51 if (message->default_begin_cs) {
52 bt_clock_snapshot_recycle(message->default_begin_cs);
53 message->default_begin_cs = NULL;
54 }
55
56 if (message->default_end_cs) {
57 bt_clock_snapshot_recycle(message->default_end_cs);
58 message->default_end_cs = NULL;
59 }
60
61 g_free(message);
62 }
63
64 static inline
65 struct bt_message *create_discarded_items_message(
66 struct bt_self_message_iterator *self_msg_iter,
67 enum bt_message_type type, struct bt_stream *stream,
68 bool with_cs,
69 uint64_t beginning_raw_value, uint64_t end_raw_value)
70 {
71 struct bt_message_discarded_items *message;
72 struct bt_stream_class *stream_class;
73 bool has_support;
74 bool need_cs;
75
76 BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator");
77 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
78 stream_class = bt_stream_borrow_class(stream);
79 BT_ASSERT(stream_class);
80
81 if (type == BT_MESSAGE_TYPE_DISCARDED_EVENTS) {
82 has_support = stream_class->supports_discarded_events;
83 need_cs = stream_class->discarded_events_have_default_clock_snapshots;
84 } else {
85 has_support = stream_class->supports_discarded_packets;
86 need_cs = stream_class->discarded_packets_have_default_clock_snapshots;
87 }
88
89 BT_ASSERT_PRE(has_support,
90 "Stream class does not support discarded events or packets: "
91 "type=%s, %![stream-]+s, %![sc-]+S",
92 bt_message_type_string(type), stream, stream_class);
93 BT_ASSERT_PRE(need_cs ? with_cs : true,
94 "Unexpected stream class configuration when creating "
95 "a discarded events or discarded packets message: "
96 "default clock snapshots are needed, but none was provided: "
97 "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, "
98 "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64,
99 bt_message_type_string(type), stream, stream_class,
100 with_cs, beginning_raw_value, end_raw_value);
101 BT_ASSERT_PRE(!need_cs ? !with_cs : true,
102 "Unexpected stream class configuration when creating "
103 "a discarded events or discarded packets message: "
104 "no default clock snapshots are needed, but two were provided: "
105 "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, "
106 "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64,
107 bt_message_type_string(type), stream, stream_class,
108 with_cs, beginning_raw_value, end_raw_value);
109 BT_LIB_LOGD("Creating discarded items message object: "
110 "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, "
111 "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64,
112 bt_message_type_string(type), stream, stream_class,
113 with_cs, beginning_raw_value, end_raw_value);
114 message = g_new0(struct bt_message_discarded_items, 1);
115 if (!message) {
116 BT_LOGE_STR("Failed to allocate one discarded items message.");
117 goto error;
118 }
119
120 bt_message_init(&message->parent, type,
121 destroy_discarded_items_message, NULL);
122 message->stream = stream;
123 bt_object_get_no_null_check(message->stream);
124
125 if (with_cs) {
126 BT_ASSERT(stream_class->default_clock_class);
127 message->default_begin_cs = bt_clock_snapshot_create(
128 stream_class->default_clock_class);
129 if (!message->default_begin_cs) {
130 goto error;
131 }
132
133 bt_clock_snapshot_set_raw_value(message->default_begin_cs,
134 beginning_raw_value);
135
136 message->default_end_cs = bt_clock_snapshot_create(
137 stream_class->default_clock_class);
138 if (!message->default_end_cs) {
139 goto error;
140 }
141
142 bt_clock_snapshot_set_raw_value(message->default_end_cs,
143 end_raw_value);
144 }
145
146 bt_property_uint_init(&message->count,
147 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0);
148 BT_LIB_LOGD("Created discarded items message object: "
149 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message,
150 stream, stream_class);
151
152 return (void *) &message->parent;
153
154 error:
155 return NULL;
156 }
157
158 static inline
159 struct bt_stream *borrow_discarded_items_message_stream(
160 struct bt_message *message)
161 {
162 struct bt_message_discarded_items *disc_items_msg = (void *) message;
163
164 BT_ASSERT(message);
165 return disc_items_msg->stream;
166 }
167
168 static inline
169 void set_discarded_items_message_count(struct bt_message *message,
170 uint64_t count)
171 {
172 struct bt_message_discarded_items *disc_items_msg = (void *) message;
173
174 BT_ASSERT(message);
175 BT_ASSERT_PRE_HOT(message, "Message", ": %!+n", message);
176 bt_property_uint_set(&disc_items_msg->count, count);
177 }
178
179 static inline
180 enum bt_property_availability get_discarded_items_message_count(
181 const struct bt_message *message, uint64_t *count)
182 {
183 struct bt_message_discarded_items *disc_items_msg = (void *) message;
184
185 BT_ASSERT_PRE_NON_NULL(count, "Count (output)");
186 BT_ASSERT(message);
187 *count = disc_items_msg->count.value;
188 return disc_items_msg->count.base.avail;
189 }
190
191 static inline
192 const struct bt_clock_snapshot *
193 borrow_discarded_items_message_beginning_default_clock_snapshot_const(
194 const struct bt_message *message)
195 {
196 struct bt_message_discarded_items *disc_items_msg = (void *) message;
197
198 BT_ASSERT(message);
199 BT_ASSERT_PRE(disc_items_msg->stream->class->default_clock_class,
200 "Message's stream's class has no default clock class: "
201 "%![msg-]+n, %![sc-]+S",
202 message, disc_items_msg->stream->class);
203 return disc_items_msg->default_begin_cs;
204 }
205
206 static inline
207 const struct bt_clock_snapshot *
208 borrow_discarded_items_message_end_default_clock_snapshot_const(
209 const struct bt_message *message)
210 {
211 struct bt_message_discarded_items *disc_items_msg = (void *) message;
212
213 BT_ASSERT(message);
214 BT_ASSERT_PRE(disc_items_msg->stream->class->default_clock_class,
215 "Message's stream's class has no default clock class: "
216 "%![msg-]+n, %![sc-]+S",
217 message, disc_items_msg->stream->class);
218 return disc_items_msg->default_end_cs;
219 }
220
221 struct bt_message *bt_message_discarded_events_create(
222 struct bt_self_message_iterator *message_iterator,
223 const struct bt_stream *stream)
224 {
225 return create_discarded_items_message(message_iterator,
226 BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream,
227 false, 0, 0);
228 }
229
230 struct bt_message *bt_message_discarded_events_create_with_default_clock_snapshots(
231 struct bt_self_message_iterator *message_iterator,
232 const struct bt_stream *stream, uint64_t beginning_raw_value,
233 uint64_t end_raw_value)
234 {
235 return create_discarded_items_message(message_iterator,
236 BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream,
237 true, beginning_raw_value, end_raw_value);
238 }
239
240 struct bt_stream *bt_message_discarded_events_borrow_stream(
241 struct bt_message *message)
242 {
243 BT_ASSERT_PRE_NON_NULL(message, "Message");
244 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
245 return borrow_discarded_items_message_stream(message);
246 }
247
248 void bt_message_discarded_events_set_count(struct bt_message *message,
249 uint64_t count)
250 {
251 BT_ASSERT_PRE_NON_NULL(message, "Message");
252 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
253 set_discarded_items_message_count(message, count);
254 }
255
256 const struct bt_clock_snapshot *
257 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
258 const struct bt_message *msg)
259 {
260 BT_ASSERT_PRE_NON_NULL(msg, "Message");
261 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
262 return borrow_discarded_items_message_beginning_default_clock_snapshot_const(
263 msg);
264 }
265
266 const struct bt_clock_snapshot *
267 bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
268 const struct bt_message *msg)
269 {
270 BT_ASSERT_PRE_NON_NULL(msg, "Message");
271 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
272 return borrow_discarded_items_message_end_default_clock_snapshot_const(
273 msg);
274 }
275
276 const struct bt_stream *
277 bt_message_discarded_events_borrow_stream_const(const struct bt_message *message)
278 {
279 return (void *) bt_message_discarded_events_borrow_stream(
280 (void *) message);
281 }
282
283 enum bt_property_availability bt_message_discarded_events_get_count(
284 const struct bt_message *message, uint64_t *count)
285 {
286 BT_ASSERT_PRE_NON_NULL(message, "Message");
287 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
288 return get_discarded_items_message_count(message, count);
289 }
290
291 struct bt_message *bt_message_discarded_packets_create(
292 struct bt_self_message_iterator *message_iterator,
293 const struct bt_stream *stream)
294 {
295 return create_discarded_items_message(message_iterator,
296 BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream,
297 false, 0, 0);
298 }
299
300 struct bt_message *bt_message_discarded_packets_create_with_default_clock_snapshots(
301 struct bt_self_message_iterator *message_iterator,
302 const struct bt_stream *stream, uint64_t beginning_raw_value,
303 uint64_t end_raw_value)
304 {
305 return create_discarded_items_message(message_iterator,
306 BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream,
307 true, beginning_raw_value, end_raw_value);
308 }
309
310 struct bt_stream *bt_message_discarded_packets_borrow_stream(
311 struct bt_message *message)
312 {
313 BT_ASSERT_PRE_NON_NULL(message, "Message");
314 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
315 return borrow_discarded_items_message_stream(message);
316 }
317
318 void bt_message_discarded_packets_set_count(struct bt_message *message,
319 uint64_t count)
320 {
321 BT_ASSERT_PRE_NON_NULL(message, "Message");
322 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
323 set_discarded_items_message_count(message, count);
324 }
325
326 const struct bt_clock_snapshot *
327 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
328 const struct bt_message *msg)
329 {
330 BT_ASSERT_PRE_NON_NULL(msg, "Message");
331 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
332 return borrow_discarded_items_message_beginning_default_clock_snapshot_const(
333 msg);
334 }
335
336 const struct bt_clock_snapshot *
337 bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
338 const struct bt_message *msg)
339 {
340 BT_ASSERT_PRE_NON_NULL(msg, "Message");
341 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
342 return borrow_discarded_items_message_end_default_clock_snapshot_const(
343 msg);
344 }
345
346 const struct bt_stream *
347 bt_message_discarded_packets_borrow_stream_const(const struct bt_message *message)
348 {
349 return (void *) bt_message_discarded_packets_borrow_stream(
350 (void *) message);
351 }
352
353 enum bt_property_availability bt_message_discarded_packets_get_count(
354 const struct bt_message *message, uint64_t *count)
355 {
356 BT_ASSERT_PRE_NON_NULL(message, "Message");
357 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
358 return get_discarded_items_message_count(message, count);
359 }
360
361 static inline
362 const struct bt_clock_class *
363 borrow_discarded_items_message_stream_class_default_clock_class(
364 const struct bt_message *msg)
365 {
366 struct bt_message_discarded_items *disc_items_msg = (void *) msg;
367
368 BT_ASSERT(msg);
369 return disc_items_msg->stream->class->default_clock_class;
370 }
371
372 const struct bt_clock_class *
373 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
374 const struct bt_message *msg)
375 {
376 BT_ASSERT_PRE_NON_NULL(msg, "Message");
377 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
378 return borrow_discarded_items_message_stream_class_default_clock_class(
379 msg);
380 }
381
382 const struct bt_clock_class *
383 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
384 const struct bt_message *msg)
385 {
386 BT_ASSERT_PRE_NON_NULL(msg, "Message");
387 BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
388 return borrow_discarded_items_message_stream_class_default_clock_class(
389 msg);
390 }
This page took 0.03717 seconds and 4 git commands to generate.