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