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