Commit | Line | Data |
---|---|---|
70f1d471 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 | ||
23 | #define BT_LOG_TAG "MSG-DISCARDED-ITEMS" | |
24 | #include <babeltrace/lib-logging-internal.h> | |
25 | ||
26 | #include <babeltrace/assert-pre-internal.h> | |
27 | #include <babeltrace/object-internal.h> | |
28 | #include <babeltrace/compiler-internal.h> | |
29 | #include <babeltrace/trace-ir/clock-class.h> | |
30 | #include <babeltrace/trace-ir/clock-snapshot-internal.h> | |
31 | #include <babeltrace/trace-ir/stream-class-internal.h> | |
32 | #include <babeltrace/trace-ir/stream-internal.h> | |
33 | #include <babeltrace/property-internal.h> | |
34 | #include <babeltrace/graph/message-internal.h> | |
35 | #include <babeltrace/graph/message-discarded-items-internal.h> | |
36 | #include <babeltrace/graph/message-discarded-events.h> | |
37 | #include <babeltrace/graph/message-discarded-events-const.h> | |
38 | ||
39 | static | |
40 | void destroy_discarded_items_message(struct bt_object *obj) | |
41 | { | |
42 | struct bt_message_discarded_items *message = (void *) obj; | |
43 | ||
44 | BT_LIB_LOGD("Destroying discarded items message: %!+n", message); | |
45 | BT_LIB_LOGD("Putting stream: %!+s", message->stream); | |
46 | BT_OBJECT_PUT_REF_AND_RESET(message->stream); | |
47 | ||
48 | if (message->default_begin_cs) { | |
49 | bt_clock_snapshot_recycle(message->default_begin_cs); | |
50 | message->default_begin_cs = NULL; | |
51 | } | |
52 | ||
53 | if (message->default_end_cs) { | |
54 | bt_clock_snapshot_recycle(message->default_end_cs); | |
55 | message->default_end_cs = NULL; | |
56 | } | |
57 | ||
58 | g_free(message); | |
59 | } | |
60 | ||
61 | static inline | |
62 | struct bt_message *create_discarded_items_message( | |
63 | struct bt_self_message_iterator *self_msg_iter, | |
64 | enum bt_message_type type, struct bt_stream *stream, | |
65 | bool with_cs, | |
66 | uint64_t beginning_raw_value, uint64_t end_raw_value) | |
67 | { | |
68 | struct bt_message_discarded_items *message; | |
69 | struct bt_stream_class *stream_class; | |
70 | ||
71 | BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator"); | |
72 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); | |
73 | stream_class = bt_stream_borrow_class(stream); | |
74 | BT_ASSERT(stream_class); | |
75 | BT_LIB_LOGD("Creating discarded items message object: " | |
76 | "type=%s, %![stream-]+s, %![sc-]+S, with-cs=%d, " | |
77 | "cs-begin-val=%" PRIu64 ", cs-end-val=%" PRIu64, | |
78 | bt_message_type_string(type), stream, stream_class, | |
79 | with_cs, beginning_raw_value, end_raw_value); | |
80 | message = g_new0(struct bt_message_discarded_items, 1); | |
81 | if (!message) { | |
82 | BT_LOGE_STR("Failed to allocate one discarded items message."); | |
83 | goto error; | |
84 | } | |
85 | ||
86 | bt_message_init(&message->parent, type, | |
87 | destroy_discarded_items_message, NULL); | |
88 | message->stream = stream; | |
89 | bt_object_get_no_null_check(message->stream); | |
90 | ||
91 | if (with_cs) { | |
92 | message->default_begin_cs = bt_clock_snapshot_create( | |
93 | stream_class->default_clock_class); | |
94 | if (!message->default_begin_cs) { | |
95 | goto error; | |
96 | } | |
97 | ||
98 | bt_clock_snapshot_set_raw_value(message->default_begin_cs, | |
99 | beginning_raw_value); | |
100 | ||
101 | message->default_end_cs = bt_clock_snapshot_create( | |
102 | stream_class->default_clock_class); | |
103 | if (!message->default_end_cs) { | |
104 | goto error; | |
105 | } | |
106 | ||
107 | bt_clock_snapshot_set_raw_value(message->default_end_cs, | |
108 | end_raw_value); | |
109 | } | |
110 | ||
111 | bt_property_uint_init(&message->count, | |
112 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0); | |
113 | BT_LIB_LOGD("Created discarded items message object: " | |
114 | "%![msg-]+n, %![stream-]+s, %![sc-]+S", message, | |
115 | stream, stream_class); | |
116 | ||
117 | return (void *) &message->parent; | |
118 | ||
119 | error: | |
120 | return NULL; | |
121 | } | |
122 | ||
123 | static inline | |
124 | struct bt_stream *borrow_discarded_items_message_stream( | |
125 | struct bt_message *message) | |
126 | { | |
127 | struct bt_message_discarded_items *disc_items_msg = (void *) message; | |
128 | ||
129 | BT_ASSERT(message); | |
130 | return disc_items_msg->stream; | |
131 | } | |
132 | ||
133 | static inline | |
134 | void set_discarded_items_message_count(struct bt_message *message, | |
135 | uint64_t count) | |
136 | { | |
137 | struct bt_message_discarded_items *disc_items_msg = (void *) message; | |
138 | ||
139 | BT_ASSERT(message); | |
140 | BT_ASSERT_PRE_HOT(message, "Message", ": %!+n", message); | |
141 | bt_property_uint_set(&disc_items_msg->count, count); | |
142 | } | |
143 | ||
144 | static inline | |
145 | enum bt_property_availability get_discarded_items_message_count( | |
146 | const struct bt_message *message, uint64_t *count) | |
147 | { | |
148 | struct bt_message_discarded_items *disc_items_msg = (void *) message; | |
149 | ||
150 | BT_ASSERT_PRE_NON_NULL(count, "Count (output)"); | |
151 | BT_ASSERT(message); | |
152 | *count = disc_items_msg->count.value; | |
153 | return disc_items_msg->count.base.avail; | |
154 | } | |
155 | ||
156 | static inline | |
157 | enum bt_clock_snapshot_state | |
158 | borrow_discarded_items_message_default_beginning_clock_snapshot_const( | |
159 | const struct bt_message *message, | |
160 | const struct bt_clock_snapshot **snapshot) | |
161 | { | |
162 | struct bt_message_discarded_items *disc_items_msg = (void *) message; | |
163 | ||
164 | BT_ASSERT(message); | |
165 | BT_ASSERT_PRE(disc_items_msg->stream->class->default_clock_class, | |
166 | "Message's stream's class has no default clock class: " | |
167 | "%![msg-]+n, %![sc-]+S", | |
168 | message, disc_items_msg->stream->class); | |
169 | BT_ASSERT_PRE_NON_NULL(snapshot, "Clock snapshot (output)"); | |
170 | *snapshot = disc_items_msg->default_begin_cs; | |
171 | return BT_CLOCK_SNAPSHOT_STATE_KNOWN; | |
172 | } | |
173 | ||
174 | static inline | |
175 | enum bt_clock_snapshot_state | |
176 | borrow_discarded_items_message_default_end_clock_snapshot_const( | |
177 | const struct bt_message *message, | |
178 | const struct bt_clock_snapshot **snapshot) | |
179 | { | |
180 | struct bt_message_discarded_items *disc_items_msg = (void *) message; | |
181 | ||
182 | BT_ASSERT(message); | |
183 | BT_ASSERT_PRE(disc_items_msg->stream->class->default_clock_class, | |
184 | "Message's stream's class has no default clock class: " | |
185 | "%![msg-]+n, %![sc-]+S", | |
186 | message, disc_items_msg->stream->class); | |
187 | BT_ASSERT_PRE_NON_NULL(snapshot, "Clock snapshot (output)"); | |
188 | *snapshot = disc_items_msg->default_end_cs; | |
189 | return BT_CLOCK_SNAPSHOT_STATE_KNOWN; | |
190 | } | |
191 | ||
192 | struct bt_message *bt_message_discarded_events_create( | |
193 | struct bt_self_message_iterator *message_iterator, | |
194 | struct bt_stream *stream) | |
195 | { | |
196 | return create_discarded_items_message(message_iterator, | |
197 | BT_MESSAGE_TYPE_DISCARDED_EVENTS, stream, | |
198 | false, 0, 0); | |
199 | } | |
200 | ||
201 | struct bt_message *bt_message_discarded_events_create_with_default_clock_snapshots( | |
202 | struct bt_self_message_iterator *message_iterator, | |
203 | struct bt_stream *stream, uint64_t beginning_raw_value, | |
204 | uint64_t end_raw_value) | |
205 | { | |
206 | return create_discarded_items_message(message_iterator, | |
207 | BT_MESSAGE_TYPE_DISCARDED_EVENTS, stream, | |
208 | true, beginning_raw_value, end_raw_value); | |
209 | } | |
210 | ||
211 | struct bt_stream *bt_message_discarded_events_borrow_stream( | |
212 | struct bt_message *message) | |
213 | { | |
214 | BT_ASSERT_PRE_NON_NULL(message, "Message"); | |
215 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS); | |
216 | return borrow_discarded_items_message_stream(message); | |
217 | } | |
218 | ||
219 | void bt_message_discarded_events_set_count(struct bt_message *message, | |
220 | uint64_t count) | |
221 | { | |
222 | BT_ASSERT_PRE_NON_NULL(message, "Message"); | |
223 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS); | |
224 | set_discarded_items_message_count(message, count); | |
225 | } | |
226 | ||
227 | enum bt_clock_snapshot_state | |
228 | bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const( | |
229 | const struct bt_message *msg, | |
230 | const struct bt_clock_snapshot **snapshot) | |
231 | { | |
232 | BT_ASSERT_PRE_NON_NULL(msg, "Message"); | |
233 | BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS); | |
234 | return borrow_discarded_items_message_default_beginning_clock_snapshot_const( | |
235 | msg, snapshot); | |
236 | } | |
237 | ||
238 | enum bt_clock_snapshot_state | |
239 | bt_message_discarded_events_borrow_default_end_clock_snapshot_const( | |
240 | const struct bt_message *msg, | |
241 | const struct bt_clock_snapshot **snapshot) | |
242 | { | |
243 | BT_ASSERT_PRE_NON_NULL(msg, "Message"); | |
244 | BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_DISCARDED_EVENTS); | |
245 | return borrow_discarded_items_message_default_end_clock_snapshot_const( | |
246 | msg, snapshot); | |
247 | } | |
248 | ||
249 | const struct bt_stream * | |
250 | bt_message_discarded_events_borrow_stream_const(const struct bt_message *message) | |
251 | { | |
252 | return (void *) bt_message_discarded_events_borrow_stream( | |
253 | (void *) message); | |
254 | } | |
255 | ||
256 | enum bt_property_availability bt_message_discarded_events_get_count( | |
257 | const struct bt_message *message, uint64_t *count) | |
258 | { | |
259 | BT_ASSERT_PRE_NON_NULL(message, "Message"); | |
260 | BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_DISCARDED_EVENTS); | |
261 | return get_discarded_items_message_count(message, count); | |
262 | } |