cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[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
d98421f2 12#include "lib/assert-cond.h"
578e048b
MJ
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
1778c2a4
PP
24#define BT_ASSERT_PRE_MSG_IS_DISC_EVENTS(_msg) \
25 BT_ASSERT_PRE_MSG_HAS_TYPE("message", (_msg), \
26 "discarded-events", BT_MESSAGE_TYPE_DISCARDED_EVENTS);
27
28#define BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(_msg) \
29 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg), \
30 "discarded-events", BT_MESSAGE_TYPE_DISCARDED_EVENTS);
31
32#define BT_ASSERT_PRE_MSG_IS_DISC_PACKETS(_msg) \
33 BT_ASSERT_PRE_MSG_HAS_TYPE("message", (_msg), \
34 "discarded-packets", BT_MESSAGE_TYPE_DISCARDED_PACKETS);
35
36#define BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(_msg) \
37 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg), \
38 "discarded-packets", BT_MESSAGE_TYPE_DISCARDED_PACKETS);
39
40#define BT_ASSERT_PRE_DEV_COUNT_OUTPUT_NON_NULL(_count) \
41 BT_ASSERT_PRE_DEV_NON_NULL("count-output", count, \
42 "Count (output)");
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,
1778c2a4
PP
71 uint64_t beginning_raw_value, uint64_t end_raw_value,
72 const char *api_func,
73 const char *sc_supports_disc_precond_id)
4c833281
PP
74{
75 struct bt_message_discarded_items *message;
76 struct bt_stream_class *stream_class;
2e90378a 77 bool has_support;
8cc5f12b 78 bool need_cs;
4c833281 79
1778c2a4
PP
80 BT_ASSERT_PRE_MSG_ITER_NON_NULL_FROM_FUNC(api_func, self_msg_iter);
81 BT_ASSERT_PRE_STREAM_NON_NULL_FROM_FUNC(api_func, stream);
4c833281
PP
82 stream_class = bt_stream_borrow_class(stream);
83 BT_ASSERT(stream_class);
2e90378a
PP
84
85 if (type == BT_MESSAGE_TYPE_DISCARDED_EVENTS) {
86 has_support = stream_class->supports_discarded_events;
8cc5f12b 87 need_cs = stream_class->discarded_events_have_default_clock_snapshots;
2e90378a
PP
88 } else {
89 has_support = stream_class->supports_discarded_packets;
8cc5f12b 90 need_cs = stream_class->discarded_packets_have_default_clock_snapshots;
2e90378a
PP
91 }
92
1778c2a4
PP
93 BT_ASSERT_PRE_FROM_FUNC(api_func, sc_supports_disc_precond_id,
94 has_support,
2e90378a
PP
95 "Stream class does not support discarded events or packets: "
96 "type=%s, %![stream-]+s, %![sc-]+S",
1e123ed6 97 bt_common_message_type_string(type), stream, stream_class);
1778c2a4
PP
98 BT_ASSERT_PRE_FROM_FUNC(api_func, "with-default-clock-snapshots",
99 need_cs ? with_cs : true,
2e90378a 100 "Unexpected stream class configuration when creating "
8cc5f12b
PP
101 "a discarded events or discarded packets message: "
102 "default clock snapshots are needed, but none was provided: "
103 "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, "
104 "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64,
1e123ed6 105 bt_common_message_type_string(type), stream, stream_class,
8cc5f12b 106 with_cs, beginning_raw_value, end_raw_value);
1778c2a4
PP
107 BT_ASSERT_PRE_FROM_FUNC(api_func, "without-default-clock-snapshots",
108 !need_cs ? !with_cs : true,
8cc5f12b
PP
109 "Unexpected stream class configuration when creating "
110 "a discarded events or discarded packets message: "
111 "no default clock snapshots are needed, but two were provided: "
aa12059b
PP
112 "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, "
113 "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64,
1e123ed6 114 bt_common_message_type_string(type), stream, stream_class,
aa12059b 115 with_cs, beginning_raw_value, end_raw_value);
4c833281
PP
116 BT_LIB_LOGD("Creating discarded items message object: "
117 "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, "
118 "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64,
1e123ed6 119 bt_common_message_type_string(type), stream, stream_class,
4c833281
PP
120 with_cs, beginning_raw_value, end_raw_value);
121 message = g_new0(struct bt_message_discarded_items, 1);
122 if (!message) {
870631a2
PP
123 BT_LIB_LOGE_APPEND_CAUSE(
124 "Failed to allocate one discarded items message.");
4c833281
PP
125 goto error;
126 }
127
128 bt_message_init(&message->parent, type,
129 destroy_discarded_items_message, NULL);
130 message->stream = stream;
6871026b 131 bt_object_get_ref_no_null_check(message->stream);
4c833281
PP
132
133 if (with_cs) {
aa12059b 134 BT_ASSERT(stream_class->default_clock_class);
4c833281
PP
135 message->default_begin_cs = bt_clock_snapshot_create(
136 stream_class->default_clock_class);
137 if (!message->default_begin_cs) {
138 goto error;
139 }
140
141 bt_clock_snapshot_set_raw_value(message->default_begin_cs,
142 beginning_raw_value);
143
144 message->default_end_cs = bt_clock_snapshot_create(
145 stream_class->default_clock_class);
146 if (!message->default_end_cs) {
147 goto error;
148 }
149
150 bt_clock_snapshot_set_raw_value(message->default_end_cs,
151 end_raw_value);
152 }
153
154 bt_property_uint_init(&message->count,
155 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0);
156 BT_LIB_LOGD("Created discarded items message object: "
157 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message,
158 stream, stream_class);
159
160 return (void *) &message->parent;
161
162error:
163 return NULL;
164}
165
166static inline
167struct bt_stream *borrow_discarded_items_message_stream(
168 struct bt_message *message)
169{
170 struct bt_message_discarded_items *disc_items_msg = (void *) message;
171
98b15851 172 BT_ASSERT_DBG(message);
4c833281
PP
173 return disc_items_msg->stream;
174}
175
176static inline
177void set_discarded_items_message_count(struct bt_message *message,
178 uint64_t count)
179{
180 struct bt_message_discarded_items *disc_items_msg = (void *) message;
181
182 BT_ASSERT(message);
4c833281
PP
183 bt_property_uint_set(&disc_items_msg->count, count);
184}
185
186static inline
187enum bt_property_availability get_discarded_items_message_count(
188 const struct bt_message *message, uint64_t *count)
189{
190 struct bt_message_discarded_items *disc_items_msg = (void *) message;
191
98b15851 192 BT_ASSERT_DBG(message);
1778c2a4 193 BT_ASSERT_DBG(count);
4c833281
PP
194 *count = disc_items_msg->count.value;
195 return disc_items_msg->count.base.avail;
196}
197
198static inline
0cbc2c33 199const struct bt_clock_snapshot *
9b24b6aa 200borrow_discarded_items_message_beginning_default_clock_snapshot_const(
1778c2a4 201 const struct bt_message *message, const char *api_func)
4c833281
PP
202{
203 struct bt_message_discarded_items *disc_items_msg = (void *) message;
204
98b15851 205 BT_ASSERT_DBG(message);
1778c2a4 206 BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS_FROM_FUNC(api_func, message,
d5b13b9b 207 disc_items_msg->stream->class);
0cbc2c33 208 return disc_items_msg->default_begin_cs;
4c833281
PP
209}
210
211static inline
0cbc2c33 212const struct bt_clock_snapshot *
9b24b6aa 213borrow_discarded_items_message_end_default_clock_snapshot_const(
1778c2a4 214 const struct bt_message *message, const char *api_func)
4c833281
PP
215{
216 struct bt_message_discarded_items *disc_items_msg = (void *) message;
217
98b15851 218 BT_ASSERT_DBG(message);
1778c2a4 219 BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS_FROM_FUNC(api_func, message,
d5b13b9b 220 disc_items_msg->stream->class);
0cbc2c33 221 return disc_items_msg->default_end_cs;
4c833281
PP
222}
223
1778c2a4
PP
224#define SC_SUPPORTS_DISC_PRECOND_ID(_item_type) \
225 "stream-class-supports-discarded-" _item_type
226
1353b066 227BT_EXPORT
4c833281
PP
228struct bt_message *bt_message_discarded_events_create(
229 struct bt_self_message_iterator *message_iterator,
58085ca4 230 const struct bt_stream *stream)
4c833281 231{
17f3083a
SM
232 BT_ASSERT_PRE_DEV_NO_ERROR();
233
4c833281 234 return create_discarded_items_message(message_iterator,
58085ca4 235 BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream,
1778c2a4
PP
236 false, 0, 0, __func__,
237 SC_SUPPORTS_DISC_PRECOND_ID("events"));
4c833281
PP
238}
239
1353b066 240BT_EXPORT
4c833281
PP
241struct bt_message *bt_message_discarded_events_create_with_default_clock_snapshots(
242 struct bt_self_message_iterator *message_iterator,
58085ca4 243 const struct bt_stream *stream, uint64_t beginning_raw_value,
4c833281
PP
244 uint64_t end_raw_value)
245{
17f3083a 246 BT_ASSERT_PRE_DEV_NO_ERROR();
4030a924 247 BT_ASSERT_PRE_MSG_CS_BEGIN_LE_END(message_iterator,
d5b13b9b 248 beginning_raw_value, end_raw_value);
17f3083a 249
4c833281 250 return create_discarded_items_message(message_iterator,
58085ca4 251 BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream,
1778c2a4
PP
252 true, beginning_raw_value, end_raw_value, __func__,
253 SC_SUPPORTS_DISC_PRECOND_ID("events"));
4c833281
PP
254}
255
1353b066 256BT_EXPORT
4c833281
PP
257struct bt_stream *bt_message_discarded_events_borrow_stream(
258 struct bt_message *message)
259{
d5b13b9b 260 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
1778c2a4 261 BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(message);
4c833281
PP
262 return borrow_discarded_items_message_stream(message);
263}
264
1353b066 265BT_EXPORT
4c833281
PP
266void bt_message_discarded_events_set_count(struct bt_message *message,
267 uint64_t count)
268{
d5b13b9b 269 BT_ASSERT_PRE_MSG_NON_NULL(message);
1778c2a4
PP
270 BT_ASSERT_PRE_MSG_IS_DISC_EVENTS(message);
271 BT_ASSERT_PRE_DEV_MSG_HOT(message);
272 BT_ASSERT_PRE("count-gt-0", count > 0, "Discarded event count is 0.");
4c833281
PP
273 set_discarded_items_message_count(message, count);
274}
275
1353b066 276BT_EXPORT
0cbc2c33 277const struct bt_clock_snapshot *
9b24b6aa 278bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 279 const struct bt_message *msg)
4c833281 280{
d5b13b9b 281 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
1778c2a4 282 BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(msg);
9b24b6aa 283 return borrow_discarded_items_message_beginning_default_clock_snapshot_const(
1778c2a4 284 msg, __func__);
4c833281
PP
285}
286
1353b066 287BT_EXPORT
0cbc2c33 288const struct bt_clock_snapshot *
9b24b6aa 289bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
0cbc2c33 290 const struct bt_message *msg)
4c833281 291{
d5b13b9b 292 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
1778c2a4 293 BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(msg);
9b24b6aa 294 return borrow_discarded_items_message_end_default_clock_snapshot_const(
1778c2a4 295 msg, __func__);
4c833281
PP
296}
297
1353b066 298BT_EXPORT
4c833281
PP
299const struct bt_stream *
300bt_message_discarded_events_borrow_stream_const(const struct bt_message *message)
301{
302 return (void *) bt_message_discarded_events_borrow_stream(
303 (void *) message);
304}
305
1353b066 306BT_EXPORT
4c833281
PP
307enum bt_property_availability bt_message_discarded_events_get_count(
308 const struct bt_message *message, uint64_t *count)
309{
d5b13b9b 310 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
1778c2a4
PP
311 BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(message);
312 BT_ASSERT_PRE_DEV_COUNT_OUTPUT_NON_NULL(count);
4c833281
PP
313 return get_discarded_items_message_count(message, count);
314}
4237f1f2 315
1353b066 316BT_EXPORT
4237f1f2
PP
317struct bt_message *bt_message_discarded_packets_create(
318 struct bt_self_message_iterator *message_iterator,
58085ca4 319 const struct bt_stream *stream)
4237f1f2 320{
17f3083a
SM
321 BT_ASSERT_PRE_DEV_NO_ERROR();
322
4237f1f2 323 return create_discarded_items_message(message_iterator,
58085ca4 324 BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream,
1778c2a4 325 false, 0, 0, __func__, SC_SUPPORTS_DISC_PRECOND_ID("packets"));
4237f1f2
PP
326}
327
1353b066 328BT_EXPORT
4237f1f2
PP
329struct bt_message *bt_message_discarded_packets_create_with_default_clock_snapshots(
330 struct bt_self_message_iterator *message_iterator,
58085ca4 331 const struct bt_stream *stream, uint64_t beginning_raw_value,
4237f1f2
PP
332 uint64_t end_raw_value)
333{
17f3083a 334 BT_ASSERT_PRE_DEV_NO_ERROR();
4030a924 335 BT_ASSERT_PRE_MSG_CS_BEGIN_LE_END(message_iterator,
d5b13b9b 336 beginning_raw_value, end_raw_value);
17f3083a 337
4237f1f2 338 return create_discarded_items_message(message_iterator,
58085ca4 339 BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream,
1778c2a4
PP
340 true, beginning_raw_value, end_raw_value, __func__,
341 SC_SUPPORTS_DISC_PRECOND_ID("packets"));
4237f1f2
PP
342}
343
1353b066 344BT_EXPORT
4237f1f2
PP
345struct bt_stream *bt_message_discarded_packets_borrow_stream(
346 struct bt_message *message)
347{
d5b13b9b 348 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
1778c2a4 349 BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(message);
4237f1f2
PP
350 return borrow_discarded_items_message_stream(message);
351}
352
1353b066 353BT_EXPORT
4237f1f2
PP
354void bt_message_discarded_packets_set_count(struct bt_message *message,
355 uint64_t count)
356{
d5b13b9b 357 BT_ASSERT_PRE_MSG_NON_NULL(message);
1778c2a4
PP
358 BT_ASSERT_PRE_MSG_IS_DISC_PACKETS(message);
359 BT_ASSERT_PRE_DEV_MSG_HOT(message);
360 BT_ASSERT_PRE("count-gt-0", count > 0, "Discarded packet count is 0.");
4237f1f2
PP
361 set_discarded_items_message_count(message, count);
362}
363
1353b066 364BT_EXPORT
0cbc2c33 365const struct bt_clock_snapshot *
9b24b6aa 366bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 367 const struct bt_message *msg)
4237f1f2 368{
d5b13b9b 369 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
1778c2a4 370 BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(msg);
9b24b6aa 371 return borrow_discarded_items_message_beginning_default_clock_snapshot_const(
1778c2a4 372 msg, __func__);
4237f1f2
PP
373}
374
1353b066 375BT_EXPORT
0cbc2c33 376const struct bt_clock_snapshot *
9b24b6aa 377bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
0cbc2c33 378 const struct bt_message *msg)
4237f1f2 379{
d5b13b9b 380 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
1778c2a4 381 BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(msg);
9b24b6aa 382 return borrow_discarded_items_message_end_default_clock_snapshot_const(
1778c2a4 383 msg, __func__);
4237f1f2
PP
384}
385
1353b066 386BT_EXPORT
4237f1f2
PP
387const struct bt_stream *
388bt_message_discarded_packets_borrow_stream_const(const struct bt_message *message)
389{
390 return (void *) bt_message_discarded_packets_borrow_stream(
391 (void *) message);
392}
393
1353b066 394BT_EXPORT
4237f1f2
PP
395enum bt_property_availability bt_message_discarded_packets_get_count(
396 const struct bt_message *message, uint64_t *count)
397{
d5b13b9b 398 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
1778c2a4
PP
399 BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(message);
400 BT_ASSERT_PRE_DEV_COUNT_OUTPUT_NON_NULL(count);
4237f1f2
PP
401 return get_discarded_items_message_count(message, count);
402}
33931ab8
PP
403
404static inline
405const struct bt_clock_class *
406borrow_discarded_items_message_stream_class_default_clock_class(
407 const struct bt_message *msg)
408{
409 struct bt_message_discarded_items *disc_items_msg = (void *) msg;
410
98b15851 411 BT_ASSERT_DBG(msg);
33931ab8
PP
412 return disc_items_msg->stream->class->default_clock_class;
413}
414
1353b066 415BT_EXPORT
33931ab8
PP
416const struct bt_clock_class *
417bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
418 const struct bt_message *msg)
419{
d5b13b9b 420 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
1778c2a4 421 BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(msg);
33931ab8
PP
422 return borrow_discarded_items_message_stream_class_default_clock_class(
423 msg);
424}
425
1353b066 426BT_EXPORT
33931ab8
PP
427const struct bt_clock_class *
428bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
429 const struct bt_message *msg)
430{
d5b13b9b 431 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
1778c2a4 432 BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(msg);
33931ab8
PP
433 return borrow_discarded_items_message_stream_class_default_clock_class(
434 msg);
435}
This page took 0.096935 seconds and 5 git commands to generate.