Commit | Line | Data |
---|---|---|
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 |
44 | static |
45 | void 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 | ||
66 | static inline | |
67 | struct 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", | |
97 | bt_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, | |
105 | bt_message_type_string(type), stream, stream_class, | |
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, | |
114 | bt_message_type_string(type), stream, stream_class, | |
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, | |
119 | bt_message_type_string(type), stream, stream_class, | |
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 | ||
162 | error: | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | static inline | |
167 | struct 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 | ||
176 | static inline | |
177 | void 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 | ||
186 | static inline | |
187 | enum 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 | ||
198 | static inline | |
0cbc2c33 | 199 | const struct bt_clock_snapshot * |
9b24b6aa | 200 | borrow_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 | ||
211 | static inline | |
0cbc2c33 | 212 | const struct bt_clock_snapshot * |
9b24b6aa | 213 | borrow_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 | ||
4c833281 PP |
227 | struct bt_message *bt_message_discarded_events_create( |
228 | struct bt_self_message_iterator *message_iterator, | |
58085ca4 | 229 | const struct bt_stream *stream) |
4c833281 | 230 | { |
17f3083a SM |
231 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
232 | ||
4c833281 | 233 | return create_discarded_items_message(message_iterator, |
58085ca4 | 234 | BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream, |
1778c2a4 PP |
235 | false, 0, 0, __func__, |
236 | SC_SUPPORTS_DISC_PRECOND_ID("events")); | |
4c833281 PP |
237 | } |
238 | ||
239 | struct bt_message *bt_message_discarded_events_create_with_default_clock_snapshots( | |
240 | struct bt_self_message_iterator *message_iterator, | |
58085ca4 | 241 | const struct bt_stream *stream, uint64_t beginning_raw_value, |
4c833281 PP |
242 | uint64_t end_raw_value) |
243 | { | |
17f3083a | 244 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
4030a924 | 245 | BT_ASSERT_PRE_MSG_CS_BEGIN_LE_END(message_iterator, |
d5b13b9b | 246 | beginning_raw_value, end_raw_value); |
17f3083a | 247 | |
4c833281 | 248 | return create_discarded_items_message(message_iterator, |
58085ca4 | 249 | BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream, |
1778c2a4 PP |
250 | true, beginning_raw_value, end_raw_value, __func__, |
251 | SC_SUPPORTS_DISC_PRECOND_ID("events")); | |
4c833281 PP |
252 | } |
253 | ||
254 | struct bt_stream *bt_message_discarded_events_borrow_stream( | |
255 | struct bt_message *message) | |
256 | { | |
d5b13b9b | 257 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
1778c2a4 | 258 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(message); |
4c833281 PP |
259 | return borrow_discarded_items_message_stream(message); |
260 | } | |
261 | ||
262 | void bt_message_discarded_events_set_count(struct bt_message *message, | |
263 | uint64_t count) | |
264 | { | |
d5b13b9b | 265 | BT_ASSERT_PRE_MSG_NON_NULL(message); |
1778c2a4 PP |
266 | BT_ASSERT_PRE_MSG_IS_DISC_EVENTS(message); |
267 | BT_ASSERT_PRE_DEV_MSG_HOT(message); | |
268 | BT_ASSERT_PRE("count-gt-0", count > 0, "Discarded event count is 0."); | |
4c833281 PP |
269 | set_discarded_items_message_count(message, count); |
270 | } | |
271 | ||
0cbc2c33 | 272 | const struct bt_clock_snapshot * |
9b24b6aa | 273 | bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( |
0cbc2c33 | 274 | const struct bt_message *msg) |
4c833281 | 275 | { |
d5b13b9b | 276 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
1778c2a4 | 277 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(msg); |
9b24b6aa | 278 | return borrow_discarded_items_message_beginning_default_clock_snapshot_const( |
1778c2a4 | 279 | msg, __func__); |
4c833281 PP |
280 | } |
281 | ||
0cbc2c33 | 282 | const struct bt_clock_snapshot * |
9b24b6aa | 283 | bt_message_discarded_events_borrow_end_default_clock_snapshot_const( |
0cbc2c33 | 284 | const struct bt_message *msg) |
4c833281 | 285 | { |
d5b13b9b | 286 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
1778c2a4 | 287 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(msg); |
9b24b6aa | 288 | return borrow_discarded_items_message_end_default_clock_snapshot_const( |
1778c2a4 | 289 | msg, __func__); |
4c833281 PP |
290 | } |
291 | ||
292 | const struct bt_stream * | |
293 | bt_message_discarded_events_borrow_stream_const(const struct bt_message *message) | |
294 | { | |
295 | return (void *) bt_message_discarded_events_borrow_stream( | |
296 | (void *) message); | |
297 | } | |
298 | ||
299 | enum bt_property_availability bt_message_discarded_events_get_count( | |
300 | const struct bt_message *message, uint64_t *count) | |
301 | { | |
d5b13b9b | 302 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
1778c2a4 PP |
303 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(message); |
304 | BT_ASSERT_PRE_DEV_COUNT_OUTPUT_NON_NULL(count); | |
4c833281 PP |
305 | return get_discarded_items_message_count(message, count); |
306 | } | |
4237f1f2 PP |
307 | |
308 | struct bt_message *bt_message_discarded_packets_create( | |
309 | struct bt_self_message_iterator *message_iterator, | |
58085ca4 | 310 | const struct bt_stream *stream) |
4237f1f2 | 311 | { |
17f3083a SM |
312 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
313 | ||
4237f1f2 | 314 | return create_discarded_items_message(message_iterator, |
58085ca4 | 315 | BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream, |
1778c2a4 | 316 | false, 0, 0, __func__, SC_SUPPORTS_DISC_PRECOND_ID("packets")); |
4237f1f2 PP |
317 | } |
318 | ||
319 | struct bt_message *bt_message_discarded_packets_create_with_default_clock_snapshots( | |
320 | struct bt_self_message_iterator *message_iterator, | |
58085ca4 | 321 | const struct bt_stream *stream, uint64_t beginning_raw_value, |
4237f1f2 PP |
322 | uint64_t end_raw_value) |
323 | { | |
17f3083a | 324 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
4030a924 | 325 | BT_ASSERT_PRE_MSG_CS_BEGIN_LE_END(message_iterator, |
d5b13b9b | 326 | beginning_raw_value, end_raw_value); |
17f3083a | 327 | |
4237f1f2 | 328 | return create_discarded_items_message(message_iterator, |
58085ca4 | 329 | BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream, |
1778c2a4 PP |
330 | true, beginning_raw_value, end_raw_value, __func__, |
331 | SC_SUPPORTS_DISC_PRECOND_ID("packets")); | |
4237f1f2 PP |
332 | } |
333 | ||
334 | struct bt_stream *bt_message_discarded_packets_borrow_stream( | |
335 | struct bt_message *message) | |
336 | { | |
d5b13b9b | 337 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
1778c2a4 | 338 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(message); |
4237f1f2 PP |
339 | return borrow_discarded_items_message_stream(message); |
340 | } | |
341 | ||
342 | void bt_message_discarded_packets_set_count(struct bt_message *message, | |
343 | uint64_t count) | |
344 | { | |
d5b13b9b | 345 | BT_ASSERT_PRE_MSG_NON_NULL(message); |
1778c2a4 PP |
346 | BT_ASSERT_PRE_MSG_IS_DISC_PACKETS(message); |
347 | BT_ASSERT_PRE_DEV_MSG_HOT(message); | |
348 | BT_ASSERT_PRE("count-gt-0", count > 0, "Discarded packet count is 0."); | |
4237f1f2 PP |
349 | set_discarded_items_message_count(message, count); |
350 | } | |
351 | ||
0cbc2c33 | 352 | const struct bt_clock_snapshot * |
9b24b6aa | 353 | bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( |
0cbc2c33 | 354 | const struct bt_message *msg) |
4237f1f2 | 355 | { |
d5b13b9b | 356 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
1778c2a4 | 357 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(msg); |
9b24b6aa | 358 | return borrow_discarded_items_message_beginning_default_clock_snapshot_const( |
1778c2a4 | 359 | msg, __func__); |
4237f1f2 PP |
360 | } |
361 | ||
0cbc2c33 | 362 | const struct bt_clock_snapshot * |
9b24b6aa | 363 | bt_message_discarded_packets_borrow_end_default_clock_snapshot_const( |
0cbc2c33 | 364 | const struct bt_message *msg) |
4237f1f2 | 365 | { |
d5b13b9b | 366 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
1778c2a4 | 367 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(msg); |
9b24b6aa | 368 | return borrow_discarded_items_message_end_default_clock_snapshot_const( |
1778c2a4 | 369 | msg, __func__); |
4237f1f2 PP |
370 | } |
371 | ||
372 | const struct bt_stream * | |
373 | bt_message_discarded_packets_borrow_stream_const(const struct bt_message *message) | |
374 | { | |
375 | return (void *) bt_message_discarded_packets_borrow_stream( | |
376 | (void *) message); | |
377 | } | |
378 | ||
379 | enum bt_property_availability bt_message_discarded_packets_get_count( | |
380 | const struct bt_message *message, uint64_t *count) | |
381 | { | |
d5b13b9b | 382 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(message); |
1778c2a4 PP |
383 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(message); |
384 | BT_ASSERT_PRE_DEV_COUNT_OUTPUT_NON_NULL(count); | |
4237f1f2 PP |
385 | return get_discarded_items_message_count(message, count); |
386 | } | |
33931ab8 PP |
387 | |
388 | static inline | |
389 | const struct bt_clock_class * | |
390 | borrow_discarded_items_message_stream_class_default_clock_class( | |
391 | const struct bt_message *msg) | |
392 | { | |
393 | struct bt_message_discarded_items *disc_items_msg = (void *) msg; | |
394 | ||
98b15851 | 395 | BT_ASSERT_DBG(msg); |
33931ab8 PP |
396 | return disc_items_msg->stream->class->default_clock_class; |
397 | } | |
398 | ||
399 | const struct bt_clock_class * | |
400 | bt_message_discarded_events_borrow_stream_class_default_clock_class_const( | |
401 | const struct bt_message *msg) | |
402 | { | |
d5b13b9b | 403 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
1778c2a4 | 404 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_EVENTS(msg); |
33931ab8 PP |
405 | return borrow_discarded_items_message_stream_class_default_clock_class( |
406 | msg); | |
407 | } | |
408 | ||
409 | const struct bt_clock_class * | |
410 | bt_message_discarded_packets_borrow_stream_class_default_clock_class_const( | |
411 | const struct bt_message *msg) | |
412 | { | |
d5b13b9b | 413 | BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg); |
1778c2a4 | 414 | BT_ASSERT_PRE_DEV_MSG_IS_DISC_PACKETS(msg); |
33931ab8 PP |
415 | return borrow_discarded_items_message_stream_class_default_clock_class( |
416 | msg); | |
417 | } |