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