lib: add precondition checks that count of discarded events/packets is greater than 0
[babeltrace.git] / src / lib / graph / message / discarded-items.c
CommitLineData
4c833281 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
4c833281 3 *
0235b0db 4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
4c833281
PP
5 */
6
350ad6c1 7#define BT_LOG_TAG "LIB/MSG-DISCARDED-ITEMS"
c2d9d9cf 8#include "lib/logging.h"
4c833281 9
c4f23e30
FD
10#include <stdbool.h>
11
578e048b
MJ
12#include "lib/assert-pre.h"
13#include "lib/object.h"
14#include "compat/compiler.h"
3fadfbc0 15#include <babeltrace2/trace-ir/clock-class.h>
578e048b
MJ
16#include "lib/trace-ir/clock-snapshot.h"
17#include "lib/trace-ir/stream-class.h"
18#include "lib/trace-ir/stream.h"
19#include "lib/property.h"
20#include "lib/graph/message/message.h"
4c833281 21
578e048b
MJ
22#include "discarded-items.h"
23
4c833281
PP
24static
25void destroy_discarded_items_message(struct bt_object *obj)
26{
27 struct bt_message_discarded_items *message = (void *) obj;
28
29 BT_LIB_LOGD("Destroying discarded items message: %!+n", message);
30 BT_LIB_LOGD("Putting stream: %!+s", message->stream);
31 BT_OBJECT_PUT_REF_AND_RESET(message->stream);
32
33 if (message->default_begin_cs) {
34 bt_clock_snapshot_recycle(message->default_begin_cs);
35 message->default_begin_cs = NULL;
36 }
37
38 if (message->default_end_cs) {
39 bt_clock_snapshot_recycle(message->default_end_cs);
40 message->default_end_cs = NULL;
41 }
42
43 g_free(message);
44}
45
46static inline
47struct bt_message *create_discarded_items_message(
48 struct bt_self_message_iterator *self_msg_iter,
49 enum bt_message_type type, struct bt_stream *stream,
50 bool with_cs,
51 uint64_t beginning_raw_value, uint64_t end_raw_value)
52{
53 struct bt_message_discarded_items *message;
54 struct bt_stream_class *stream_class;
2e90378a 55 bool has_support;
8cc5f12b 56 bool need_cs;
4c833281
PP
57
58 BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator");
59 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
60 stream_class = bt_stream_borrow_class(stream);
61 BT_ASSERT(stream_class);
2e90378a
PP
62
63 if (type == BT_MESSAGE_TYPE_DISCARDED_EVENTS) {
64 has_support = stream_class->supports_discarded_events;
8cc5f12b 65 need_cs = stream_class->discarded_events_have_default_clock_snapshots;
2e90378a
PP
66 } else {
67 has_support = stream_class->supports_discarded_packets;
8cc5f12b 68 need_cs = stream_class->discarded_packets_have_default_clock_snapshots;
2e90378a
PP
69 }
70
71 BT_ASSERT_PRE(has_support,
72 "Stream class does not support discarded events or packets: "
73 "type=%s, %![stream-]+s, %![sc-]+S",
74 bt_message_type_string(type), stream, stream_class);
8cc5f12b 75 BT_ASSERT_PRE(need_cs ? with_cs : true,
2e90378a 76 "Unexpected stream class configuration when creating "
8cc5f12b
PP
77 "a discarded events or discarded packets message: "
78 "default clock snapshots are needed, but none was provided: "
79 "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, "
80 "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64,
81 bt_message_type_string(type), stream, stream_class,
82 with_cs, beginning_raw_value, end_raw_value);
83 BT_ASSERT_PRE(!need_cs ? !with_cs : true,
84 "Unexpected stream class configuration when creating "
85 "a discarded events or discarded packets message: "
86 "no default clock snapshots are needed, but two were provided: "
aa12059b
PP
87 "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, "
88 "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64,
89 bt_message_type_string(type), stream, stream_class,
90 with_cs, beginning_raw_value, end_raw_value);
4c833281
PP
91 BT_LIB_LOGD("Creating discarded items message object: "
92 "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, "
93 "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64,
94 bt_message_type_string(type), stream, stream_class,
95 with_cs, beginning_raw_value, end_raw_value);
96 message = g_new0(struct bt_message_discarded_items, 1);
97 if (!message) {
870631a2
PP
98 BT_LIB_LOGE_APPEND_CAUSE(
99 "Failed to allocate one discarded items message.");
4c833281
PP
100 goto error;
101 }
102
103 bt_message_init(&message->parent, type,
104 destroy_discarded_items_message, NULL);
105 message->stream = stream;
6871026b 106 bt_object_get_ref_no_null_check(message->stream);
4c833281
PP
107
108 if (with_cs) {
aa12059b 109 BT_ASSERT(stream_class->default_clock_class);
4c833281
PP
110 message->default_begin_cs = bt_clock_snapshot_create(
111 stream_class->default_clock_class);
112 if (!message->default_begin_cs) {
113 goto error;
114 }
115
116 bt_clock_snapshot_set_raw_value(message->default_begin_cs,
117 beginning_raw_value);
118
119 message->default_end_cs = bt_clock_snapshot_create(
120 stream_class->default_clock_class);
121 if (!message->default_end_cs) {
122 goto error;
123 }
124
125 bt_clock_snapshot_set_raw_value(message->default_end_cs,
126 end_raw_value);
127 }
128
129 bt_property_uint_init(&message->count,
130 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0);
131 BT_LIB_LOGD("Created discarded items message object: "
132 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message,
133 stream, stream_class);
134
135 return (void *) &message->parent;
136
137error:
138 return NULL;
139}
140
141static inline
142struct bt_stream *borrow_discarded_items_message_stream(
143 struct bt_message *message)
144{
145 struct bt_message_discarded_items *disc_items_msg = (void *) message;
146
98b15851 147 BT_ASSERT_DBG(message);
4c833281
PP
148 return disc_items_msg->stream;
149}
150
151static inline
152void set_discarded_items_message_count(struct bt_message *message,
153 uint64_t count)
154{
155 struct bt_message_discarded_items *disc_items_msg = (void *) message;
156
157 BT_ASSERT(message);
bdb288b3 158 BT_ASSERT_PRE_DEV_HOT(message, "Message", ": %!+n", message);
4c833281
PP
159 bt_property_uint_set(&disc_items_msg->count, count);
160}
161
162static inline
163enum bt_property_availability get_discarded_items_message_count(
164 const struct bt_message *message, uint64_t *count)
165{
166 struct bt_message_discarded_items *disc_items_msg = (void *) message;
167
bdb288b3 168 BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)");
98b15851 169 BT_ASSERT_DBG(message);
4c833281
PP
170 *count = disc_items_msg->count.value;
171 return disc_items_msg->count.base.avail;
172}
173
174static inline
0cbc2c33 175const struct bt_clock_snapshot *
9b24b6aa 176borrow_discarded_items_message_beginning_default_clock_snapshot_const(
0cbc2c33 177 const struct bt_message *message)
4c833281
PP
178{
179 struct bt_message_discarded_items *disc_items_msg = (void *) message;
180
98b15851 181 BT_ASSERT_DBG(message);
bdb288b3 182 BT_ASSERT_PRE_DEV(disc_items_msg->stream->class->default_clock_class,
4c833281
PP
183 "Message's stream's class has no default clock class: "
184 "%![msg-]+n, %![sc-]+S",
185 message, disc_items_msg->stream->class);
0cbc2c33 186 return disc_items_msg->default_begin_cs;
4c833281
PP
187}
188
189static inline
0cbc2c33 190const struct bt_clock_snapshot *
9b24b6aa 191borrow_discarded_items_message_end_default_clock_snapshot_const(
0cbc2c33 192 const struct bt_message *message)
4c833281
PP
193{
194 struct bt_message_discarded_items *disc_items_msg = (void *) message;
195
98b15851 196 BT_ASSERT_DBG(message);
bdb288b3 197 BT_ASSERT_PRE_DEV(disc_items_msg->stream->class->default_clock_class,
4c833281
PP
198 "Message's stream's class has no default clock class: "
199 "%![msg-]+n, %![sc-]+S",
200 message, disc_items_msg->stream->class);
0cbc2c33 201 return disc_items_msg->default_end_cs;
4c833281
PP
202}
203
204struct bt_message *bt_message_discarded_events_create(
205 struct bt_self_message_iterator *message_iterator,
58085ca4 206 const struct bt_stream *stream)
4c833281 207{
17f3083a
SM
208 BT_ASSERT_PRE_DEV_NO_ERROR();
209
4c833281 210 return create_discarded_items_message(message_iterator,
58085ca4 211 BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream,
4c833281
PP
212 false, 0, 0);
213}
214
215struct bt_message *bt_message_discarded_events_create_with_default_clock_snapshots(
216 struct bt_self_message_iterator *message_iterator,
58085ca4 217 const struct bt_stream *stream, uint64_t beginning_raw_value,
4c833281
PP
218 uint64_t end_raw_value)
219{
17f3083a 220 BT_ASSERT_PRE_DEV_NO_ERROR();
5d9ef4cb 221 BT_ASSERT_PRE_BEGIN_LE_END(message_iterator, beginning_raw_value, end_raw_value);
17f3083a 222
4c833281 223 return create_discarded_items_message(message_iterator,
58085ca4 224 BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream,
4c833281
PP
225 true, beginning_raw_value, end_raw_value);
226}
227
228struct bt_stream *bt_message_discarded_events_borrow_stream(
229 struct bt_message *message)
230{
bdb288b3
PP
231 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
232 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
4c833281
PP
233 return borrow_discarded_items_message_stream(message);
234}
235
236void bt_message_discarded_events_set_count(struct bt_message *message,
237 uint64_t count)
238{
239 BT_ASSERT_PRE_NON_NULL(message, "Message");
240 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
e5a41ca3 241 BT_ASSERT_PRE(count > 0, "Discarded event count is 0.");
4c833281
PP
242 set_discarded_items_message_count(message, count);
243}
244
0cbc2c33 245const struct bt_clock_snapshot *
9b24b6aa 246bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 247 const struct bt_message *msg)
4c833281 248{
bdb288b3
PP
249 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
250 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
9b24b6aa 251 return borrow_discarded_items_message_beginning_default_clock_snapshot_const(
0cbc2c33 252 msg);
4c833281
PP
253}
254
0cbc2c33 255const struct bt_clock_snapshot *
9b24b6aa 256bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
0cbc2c33 257 const struct bt_message *msg)
4c833281 258{
bdb288b3
PP
259 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
260 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
9b24b6aa 261 return borrow_discarded_items_message_end_default_clock_snapshot_const(
0cbc2c33 262 msg);
4c833281
PP
263}
264
265const struct bt_stream *
266bt_message_discarded_events_borrow_stream_const(const struct bt_message *message)
267{
268 return (void *) bt_message_discarded_events_borrow_stream(
269 (void *) message);
270}
271
272enum bt_property_availability bt_message_discarded_events_get_count(
273 const struct bt_message *message, uint64_t *count)
274{
bdb288b3
PP
275 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
276 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
277 BT_MESSAGE_TYPE_DISCARDED_EVENTS);
4c833281
PP
278 return get_discarded_items_message_count(message, count);
279}
4237f1f2
PP
280
281struct bt_message *bt_message_discarded_packets_create(
282 struct bt_self_message_iterator *message_iterator,
58085ca4 283 const struct bt_stream *stream)
4237f1f2 284{
17f3083a
SM
285 BT_ASSERT_PRE_DEV_NO_ERROR();
286
4237f1f2 287 return create_discarded_items_message(message_iterator,
58085ca4 288 BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream,
4237f1f2
PP
289 false, 0, 0);
290}
291
292struct bt_message *bt_message_discarded_packets_create_with_default_clock_snapshots(
293 struct bt_self_message_iterator *message_iterator,
58085ca4 294 const struct bt_stream *stream, uint64_t beginning_raw_value,
4237f1f2
PP
295 uint64_t end_raw_value)
296{
17f3083a 297 BT_ASSERT_PRE_DEV_NO_ERROR();
5d9ef4cb 298 BT_ASSERT_PRE_BEGIN_LE_END(message_iterator, beginning_raw_value, end_raw_value);
17f3083a 299
4237f1f2 300 return create_discarded_items_message(message_iterator,
58085ca4 301 BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream,
4237f1f2
PP
302 true, beginning_raw_value, end_raw_value);
303}
304
305struct bt_stream *bt_message_discarded_packets_borrow_stream(
306 struct bt_message *message)
307{
bdb288b3
PP
308 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
309 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
310 BT_MESSAGE_TYPE_DISCARDED_PACKETS);
4237f1f2
PP
311 return borrow_discarded_items_message_stream(message);
312}
313
314void bt_message_discarded_packets_set_count(struct bt_message *message,
315 uint64_t count)
316{
317 BT_ASSERT_PRE_NON_NULL(message, "Message");
318 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
e5a41ca3 319 BT_ASSERT_PRE(count > 0, "Discarded packet count is 0.");
4237f1f2
PP
320 set_discarded_items_message_count(message, count);
321}
322
0cbc2c33 323const struct bt_clock_snapshot *
9b24b6aa 324bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 325 const struct bt_message *msg)
4237f1f2 326{
bdb288b3
PP
327 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
328 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
9b24b6aa 329 return borrow_discarded_items_message_beginning_default_clock_snapshot_const(
0cbc2c33 330 msg);
4237f1f2
PP
331}
332
0cbc2c33 333const struct bt_clock_snapshot *
9b24b6aa 334bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
0cbc2c33 335 const struct bt_message *msg)
4237f1f2 336{
bdb288b3
PP
337 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
338 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
9b24b6aa 339 return borrow_discarded_items_message_end_default_clock_snapshot_const(
0cbc2c33 340 msg);
4237f1f2
PP
341}
342
343const struct bt_stream *
344bt_message_discarded_packets_borrow_stream_const(const struct bt_message *message)
345{
346 return (void *) bt_message_discarded_packets_borrow_stream(
347 (void *) message);
348}
349
350enum bt_property_availability bt_message_discarded_packets_get_count(
351 const struct bt_message *message, uint64_t *count)
352{
bdb288b3
PP
353 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
354 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
355 BT_MESSAGE_TYPE_DISCARDED_PACKETS);
4237f1f2
PP
356 return get_discarded_items_message_count(message, count);
357}
33931ab8
PP
358
359static inline
360const struct bt_clock_class *
361borrow_discarded_items_message_stream_class_default_clock_class(
362 const struct bt_message *msg)
363{
364 struct bt_message_discarded_items *disc_items_msg = (void *) msg;
365
98b15851 366 BT_ASSERT_DBG(msg);
33931ab8
PP
367 return disc_items_msg->stream->class->default_clock_class;
368}
369
370const struct bt_clock_class *
371bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
372 const struct bt_message *msg)
373{
bdb288b3
PP
374 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
375 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
33931ab8
PP
376 return borrow_discarded_items_message_stream_class_default_clock_class(
377 msg);
378}
379
380const struct bt_clock_class *
381bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
382 const struct bt_message *msg)
383{
bdb288b3
PP
384 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
385 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
33931ab8
PP
386 return borrow_discarded_items_message_stream_class_default_clock_class(
387 msg);
388}
This page took 0.069574 seconds and 4 git commands to generate.