53420e9384ddea161cfb19340d5e404ff52eb7ab
[babeltrace.git] / src / lib / graph / message / discarded-items.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #define BT_LOG_TAG "LIB/MSG-DISCARDED-ITEMS"
8 #include "lib/logging.h"
9
10 #include <stdbool.h>
11
12 #include "lib/assert-cond.h"
13 #include "lib/object.h"
14 #include "compat/compiler.h"
15 #include <babeltrace2/trace-ir/clock-class.h>
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"
21
22 #include "discarded-items.h"
23
24 static
25 void 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
46 static inline
47 struct 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;
55 bool has_support;
56 bool need_cs;
57
58 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter);
59 BT_ASSERT_PRE_STREAM_NON_NULL(stream);
60 stream_class = bt_stream_borrow_class(stream);
61 BT_ASSERT(stream_class);
62
63 if (type == BT_MESSAGE_TYPE_DISCARDED_EVENTS) {
64 has_support = stream_class->supports_discarded_events;
65 need_cs = stream_class->discarded_events_have_default_clock_snapshots;
66 } else {
67 has_support = stream_class->supports_discarded_packets;
68 need_cs = stream_class->discarded_packets_have_default_clock_snapshots;
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);
75 BT_ASSERT_PRE(need_cs ? with_cs : true,
76 "Unexpected stream class configuration when creating "
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: "
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);
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) {
98 BT_LIB_LOGE_APPEND_CAUSE(
99 "Failed to allocate one discarded items message.");
100 goto error;
101 }
102
103 bt_message_init(&message->parent, type,
104 destroy_discarded_items_message, NULL);
105 message->stream = stream;
106 bt_object_get_ref_no_null_check(message->stream);
107
108 if (with_cs) {
109 BT_ASSERT(stream_class->default_clock_class);
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
137 error:
138 return NULL;
139 }
140
141 static inline
142 struct 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
147 BT_ASSERT_DBG(message);
148 return disc_items_msg->stream;
149 }
150
151 static inline
152 void 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);
158 BT_ASSERT_PRE_DEV_MSG_HOT(message);
159 bt_property_uint_set(&disc_items_msg->count, count);
160 }
161
162 static inline
163 enum 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
168 BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)");
169 BT_ASSERT_DBG(message);
170 *count = disc_items_msg->count.value;
171 return disc_items_msg->count.base.avail;
172 }
173
174 static inline
175 const struct bt_clock_snapshot *
176 borrow_discarded_items_message_beginning_default_clock_snapshot_const(
177 const struct bt_message *message)
178 {
179 struct bt_message_discarded_items *disc_items_msg = (void *) message;
180
181 BT_ASSERT_DBG(message);
182 BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS(message,
183 disc_items_msg->stream->class);
184 return disc_items_msg->default_begin_cs;
185 }
186
187 static inline
188 const struct bt_clock_snapshot *
189 borrow_discarded_items_message_end_default_clock_snapshot_const(
190 const struct bt_message *message)
191 {
192 struct bt_message_discarded_items *disc_items_msg = (void *) message;
193
194 BT_ASSERT_DBG(message);
195 BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS(message,
196 disc_items_msg->stream->class);
197 return disc_items_msg->default_end_cs;
198 }
199
200 struct bt_message *bt_message_discarded_events_create(
201 struct bt_self_message_iterator *message_iterator,
202 const struct bt_stream *stream)
203 {
204 BT_ASSERT_PRE_DEV_NO_ERROR();
205
206 return create_discarded_items_message(message_iterator,
207 BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream,
208 false, 0, 0);
209 }
210
211 struct bt_message *bt_message_discarded_events_create_with_default_clock_snapshots(
212 struct bt_self_message_iterator *message_iterator,
213 const struct bt_stream *stream, uint64_t beginning_raw_value,
214 uint64_t end_raw_value)
215 {
216 BT_ASSERT_PRE_DEV_NO_ERROR();
217 BT_ASSERT_PRE_MSG_CS_BEGIN_LE_END(message_iterator,
218 beginning_raw_value, end_raw_value);
219
220 return create_discarded_items_message(message_iterator,
221 BT_MESSAGE_TYPE_DISCARDED_EVENTS, (void *) stream,
222 true, beginning_raw_value, end_raw_value);
223 }
224
225 struct bt_stream *bt_message_discarded_events_borrow_stream(
226 struct bt_message *message)
227 {
228 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
229 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(message,
230 BT_MESSAGE_TYPE_DISCARDED_EVENTS);
231 return borrow_discarded_items_message_stream(message);
232 }
233
234 void bt_message_discarded_events_set_count(struct bt_message *message,
235 uint64_t count)
236 {
237 BT_ASSERT_PRE_MSG_NON_NULL(message);
238 BT_ASSERT_PRE_MSG_HAS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
239 BT_ASSERT_PRE(count > 0, "Discarded event count is 0.");
240 set_discarded_items_message_count(message, count);
241 }
242
243 const struct bt_clock_snapshot *
244 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
245 const struct bt_message *msg)
246 {
247 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
248 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
249 return borrow_discarded_items_message_beginning_default_clock_snapshot_const(
250 msg);
251 }
252
253 const struct bt_clock_snapshot *
254 bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
255 const struct bt_message *msg)
256 {
257 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
258 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
259 return borrow_discarded_items_message_end_default_clock_snapshot_const(
260 msg);
261 }
262
263 const struct bt_stream *
264 bt_message_discarded_events_borrow_stream_const(const struct bt_message *message)
265 {
266 return (void *) bt_message_discarded_events_borrow_stream(
267 (void *) message);
268 }
269
270 enum bt_property_availability bt_message_discarded_events_get_count(
271 const struct bt_message *message, uint64_t *count)
272 {
273 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
274 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(message,
275 BT_MESSAGE_TYPE_DISCARDED_EVENTS);
276 return get_discarded_items_message_count(message, count);
277 }
278
279 struct bt_message *bt_message_discarded_packets_create(
280 struct bt_self_message_iterator *message_iterator,
281 const struct bt_stream *stream)
282 {
283 BT_ASSERT_PRE_DEV_NO_ERROR();
284
285 return create_discarded_items_message(message_iterator,
286 BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream,
287 false, 0, 0);
288 }
289
290 struct bt_message *bt_message_discarded_packets_create_with_default_clock_snapshots(
291 struct bt_self_message_iterator *message_iterator,
292 const struct bt_stream *stream, uint64_t beginning_raw_value,
293 uint64_t end_raw_value)
294 {
295 BT_ASSERT_PRE_DEV_NO_ERROR();
296 BT_ASSERT_PRE_MSG_CS_BEGIN_LE_END(message_iterator,
297 beginning_raw_value, end_raw_value);
298
299 return create_discarded_items_message(message_iterator,
300 BT_MESSAGE_TYPE_DISCARDED_PACKETS, (void *) stream,
301 true, beginning_raw_value, end_raw_value);
302 }
303
304 struct bt_stream *bt_message_discarded_packets_borrow_stream(
305 struct bt_message *message)
306 {
307 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
308 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(message,
309 BT_MESSAGE_TYPE_DISCARDED_PACKETS);
310 return borrow_discarded_items_message_stream(message);
311 }
312
313 void bt_message_discarded_packets_set_count(struct bt_message *message,
314 uint64_t count)
315 {
316 BT_ASSERT_PRE_MSG_NON_NULL(message);
317 BT_ASSERT_PRE_MSG_HAS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
318 BT_ASSERT_PRE(count > 0, "Discarded packet count is 0.");
319 set_discarded_items_message_count(message, count);
320 }
321
322 const struct bt_clock_snapshot *
323 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
324 const struct bt_message *msg)
325 {
326 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
327 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
328 return borrow_discarded_items_message_beginning_default_clock_snapshot_const(
329 msg);
330 }
331
332 const struct bt_clock_snapshot *
333 bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
334 const struct bt_message *msg)
335 {
336 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
337 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
338 return borrow_discarded_items_message_end_default_clock_snapshot_const(
339 msg);
340 }
341
342 const struct bt_stream *
343 bt_message_discarded_packets_borrow_stream_const(const struct bt_message *message)
344 {
345 return (void *) bt_message_discarded_packets_borrow_stream(
346 (void *) message);
347 }
348
349 enum bt_property_availability bt_message_discarded_packets_get_count(
350 const struct bt_message *message, uint64_t *count)
351 {
352 BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
353 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(message,
354 BT_MESSAGE_TYPE_DISCARDED_PACKETS);
355 return get_discarded_items_message_count(message, count);
356 }
357
358 static inline
359 const struct bt_clock_class *
360 borrow_discarded_items_message_stream_class_default_clock_class(
361 const struct bt_message *msg)
362 {
363 struct bt_message_discarded_items *disc_items_msg = (void *) msg;
364
365 BT_ASSERT_DBG(msg);
366 return disc_items_msg->stream->class->default_clock_class;
367 }
368
369 const struct bt_clock_class *
370 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
371 const struct bt_message *msg)
372 {
373 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
374 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS);
375 return borrow_discarded_items_message_stream_class_default_clock_class(
376 msg);
377 }
378
379 const struct bt_clock_class *
380 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
381 const struct bt_message *msg)
382 {
383 BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
384 BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_PACKETS);
385 return borrow_discarded_items_message_stream_class_default_clock_class(
386 msg);
387 }
This page took 0.038948 seconds and 3 git commands to generate.