0cdd69f254ba83817b69b41e1c5ad724e425ce29
[babeltrace.git] / plugins / trimmer / iterator.c
1 /*
2 * iterator.c
3 *
4 * Babeltrace Trace Trimmer Iterator
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include "trimmer.h"
30 #include "iterator.h"
31 #include <babeltrace/plugin/notification/iterator.h>
32 #include <babeltrace/plugin/notification/notification.h>
33 #include <babeltrace/plugin/notification/event.h>
34 #include <babeltrace/plugin/notification/stream.h>
35 #include <babeltrace/plugin/notification/packet.h>
36 #include <babeltrace/plugin/filter.h>
37 #include <babeltrace/ctf-ir/event.h>
38 #include <babeltrace/ctf-ir/stream.h>
39 #include <babeltrace/ctf-ir/stream-class.h>
40 #include <babeltrace/ctf-ir/clock.h>
41 #include <babeltrace/ctf-ir/packet.h>
42 #include <babeltrace/ctf-ir/trace.h>
43 #include <babeltrace/ctf-ir/fields.h>
44 #include <assert.h>
45
46 static
47 void trimmer_iterator_destroy(struct bt_notification_iterator *it)
48 {
49 struct trimmer_iterator *it_data;
50
51 it_data = bt_notification_iterator_get_private_data(it);
52 assert(it_data);
53
54 if (it_data->input_iterator_group) {
55 g_ptr_array_free(it_data->input_iterator_group, TRUE);
56 }
57 bt_put(it_data->current_notification);
58 g_free(it_data);
59 }
60
61 BT_HIDDEN
62 enum bt_component_status trimmer_iterator_init(struct bt_component *component,
63 struct bt_notification_iterator *iterator)
64 {
65 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
66 enum bt_notification_iterator_status it_ret;
67 struct trimmer_iterator *it_data = g_new0(struct trimmer_iterator, 1);
68
69 if (!it_data) {
70 ret = BT_COMPONENT_STATUS_NOMEM;
71 goto end;
72 }
73
74 /* FIXME init trimmer_iterator */
75 it_ret = bt_notification_iterator_set_private_data(iterator, it_data);
76 if (it_ret) {
77 goto end;
78 }
79
80 it_ret = bt_notification_iterator_set_destroy_cb(iterator,
81 trimmer_iterator_destroy);
82 if (it_ret) {
83 ret = BT_COMPONENT_STATUS_ERROR;
84 goto end;
85 }
86
87 it_ret = bt_notification_iterator_set_next_cb(iterator,
88 trimmer_iterator_next);
89 if (it_ret) {
90 ret = BT_COMPONENT_STATUS_ERROR;
91 goto end;
92 }
93
94 it_ret = bt_notification_iterator_set_get_cb(iterator,
95 trimmer_iterator_get);
96 if (it_ret) {
97 ret = BT_COMPONENT_STATUS_ERROR;
98 goto end;
99 }
100
101 it_ret = bt_notification_iterator_set_seek_time_cb(iterator,
102 trimmer_iterator_seek_time);
103 if (it_ret) {
104 ret = BT_COMPONENT_STATUS_ERROR;
105 goto end;
106 }
107 end:
108 return ret;
109 }
110
111 BT_HIDDEN
112 struct bt_notification *trimmer_iterator_get(
113 struct bt_notification_iterator *iterator)
114 {
115 struct trimmer_iterator *trim_it;
116
117 trim_it = bt_notification_iterator_get_private_data(iterator);
118 assert(trim_it);
119
120 if (!trim_it->current_notification) {
121 enum bt_notification_iterator_status it_ret;
122
123 it_ret = trimmer_iterator_next(iterator);
124 if (it_ret) {
125 goto end;
126 }
127 }
128 end:
129 return bt_get(trim_it->current_notification);
130 }
131
132 static
133 bool evaluate_event_notification(struct bt_notification *notification,
134 struct trimmer_bound *begin, struct trimmer_bound *end)
135 {
136 int64_t ts;
137 int clock_ret;
138 struct bt_ctf_event *event = NULL;
139 bool in_range = true;
140 struct bt_ctf_clock *clock = NULL;
141 struct bt_ctf_trace *trace = NULL;
142 struct bt_ctf_stream *stream = NULL;
143 struct bt_ctf_stream_class *stream_class = NULL;
144 struct bt_ctf_clock_value *clock_value = NULL;
145
146 event = bt_notification_event_get_event(notification);
147 assert(event);
148
149 stream = bt_ctf_event_get_stream(event);
150 assert(stream);
151
152 stream_class = bt_ctf_stream_get_class(stream);
153 assert(stream_class);
154
155 trace = bt_ctf_stream_class_get_trace(stream_class);
156 assert(trace);
157
158 /* FIXME multi-clock? */
159 clock = bt_ctf_trace_get_clock(trace, 0);
160 if (!clock) {
161 goto end;
162 }
163
164 clock_value = bt_ctf_event_get_clock_value(event, clock);
165 if (!clock_value) {
166 printf_error("Failed to retrieve clock value\n");
167 goto end;
168 }
169
170 clock_ret = bt_ctf_clock_value_get_value_ns_from_epoch(
171 clock_value, &ts);
172 if (clock_ret) {
173 printf_error("Failed to retrieve clock value timestamp\n");
174 goto end;
175 }
176
177 if (begin->set && ts < begin->value) {
178 in_range = false;
179 }
180 if (end->set && ts > end->value) {
181 in_range = false;
182 }
183 end:
184 bt_put(event);
185 bt_put(clock);
186 bt_put(trace);
187 bt_put(stream);
188 bt_put(stream_class);
189 bt_put(clock_value);
190 return in_range;
191 }
192
193 static
194 int ns_from_integer_field(struct bt_ctf_field *integer, int64_t *ns)
195 {
196 int ret = 0;
197 int is_signed;
198 uint64_t raw_clock_value;
199 struct bt_ctf_field_type *integer_type = NULL;
200 struct bt_ctf_clock *clock = NULL;
201 struct bt_ctf_clock_value *clock_value = NULL;
202
203 integer_type = bt_ctf_field_get_type(integer);
204 assert(integer_type);
205 clock = bt_ctf_field_type_integer_get_mapped_clock(integer_type);
206 if (!clock) {
207 ret = -1;
208 goto end;
209 }
210
211 is_signed = bt_ctf_field_type_integer_get_signed(integer_type);
212 if (!is_signed) {
213 ret = bt_ctf_field_unsigned_integer_get_value(integer,
214 &raw_clock_value);
215 if (ret) {
216 goto end;
217 }
218 } else {
219 /* Signed clock values are unsupported. */
220 goto end;
221 }
222
223 clock_value = bt_ctf_clock_value_create(clock, raw_clock_value);
224 if (!clock_value) {
225 goto end;
226 }
227
228 ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns);
229 end:
230 bt_put(integer_type);
231 bt_put(clock);
232 bt_put(clock_value);
233 return ret;
234 }
235
236 static
237 bool evaluate_packet_notification(struct bt_notification *notification,
238 struct trimmer_bound *begin, struct trimmer_bound *end)
239 {
240 int ret;
241 int64_t begin_ns, pkt_begin_ns, end_ns, pkt_end_ns;
242 bool in_range = true;
243 struct bt_ctf_packet *packet = NULL;
244 struct bt_ctf_field *packet_context = NULL,
245 *timestamp_begin = NULL,
246 *timestamp_end = NULL;
247
248 switch (bt_notification_get_type(notification)) {
249 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
250 packet = bt_notification_packet_begin_get_packet(notification);
251 break;
252 case BT_NOTIFICATION_TYPE_PACKET_END:
253 packet = bt_notification_packet_end_get_packet(notification);
254 break;
255 default:
256 abort();
257 }
258 assert(packet);
259
260 packet_context = bt_ctf_packet_get_context(packet);
261 if (!packet_context) {
262 goto end;
263 }
264
265 if (!bt_ctf_field_is_structure(packet_context)) {
266 goto end;
267 }
268
269 timestamp_begin = bt_ctf_field_structure_get_field(
270 packet_context, "timestamp_begin");
271 if (!timestamp_begin || !bt_ctf_field_is_integer(timestamp_begin)) {
272 goto end;
273 }
274 timestamp_end = bt_ctf_field_structure_get_field(
275 packet_context, "timestamp_end");
276 if (!timestamp_end || !bt_ctf_field_is_integer(timestamp_end)) {
277 goto end;
278 }
279
280 ret = ns_from_integer_field(timestamp_begin, &pkt_begin_ns);
281 if (ret) {
282 goto end;
283 }
284 ret = ns_from_integer_field(timestamp_end, &pkt_end_ns);
285 if (ret) {
286 goto end;
287 }
288
289 begin_ns = begin->set ? begin->value : INT64_MIN;
290 end_ns = end->set ? end->value : INT64_MAX;
291
292 /*
293 * Accept if there is any overlap between the selected region and the
294 * packet.
295 */
296 in_range = (pkt_end_ns >= begin_ns) && (pkt_begin_ns <= end_ns);
297 end:
298 bt_put(packet);
299 bt_put(packet_context);
300 bt_put(timestamp_begin);
301 bt_put(timestamp_end);
302 return in_range;
303 }
304
305 /* Return true if the notification should be forwarded. */
306 static
307 bool evaluate_notification(struct bt_notification *notification,
308 struct trimmer_bound *begin, struct trimmer_bound *end)
309 {
310 bool in_range = true;
311 enum bt_notification_type type;
312
313 type = bt_notification_get_type(notification);
314 switch (type) {
315 case BT_NOTIFICATION_TYPE_EVENT:
316 in_range = evaluate_event_notification(notification, begin,
317 end);
318 break;
319 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
320 case BT_NOTIFICATION_TYPE_PACKET_END:
321 in_range = evaluate_packet_notification(notification, begin,
322 end);
323 break;
324 default:
325 /* Accept all other notifications. */
326 break;
327 }
328 return in_range;
329 }
330
331 BT_HIDDEN
332 enum bt_notification_iterator_status trimmer_iterator_next(
333 struct bt_notification_iterator *iterator)
334 {
335 struct trimmer_iterator *trim_it = NULL;
336 struct bt_component *component = NULL;
337 struct trimmer *trimmer = NULL;
338 struct bt_notification_iterator *source_it = NULL;
339 enum bt_notification_iterator_status ret =
340 BT_NOTIFICATION_ITERATOR_STATUS_OK;;
341 enum bt_component_status component_ret;
342 bool notification_in_range = false;
343
344 trim_it = bt_notification_iterator_get_private_data(iterator);
345 assert(trim_it);
346
347 component = bt_notification_iterator_get_component(iterator);
348 assert(component);
349 trimmer = bt_component_get_private_data(component);
350 assert(trimmer);
351
352 /* FIXME, should handle input iterator groups. */
353 component_ret = bt_component_filter_get_input_iterator(component, 0,
354 &source_it);
355 assert((component_ret == BT_COMPONENT_STATUS_OK) && source_it);
356
357 while (!notification_in_range) {
358 struct bt_notification *notification;
359
360 ret = bt_notification_iterator_next(source_it);
361 if (ret != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
362 goto end;
363 }
364
365 notification = bt_notification_iterator_get_notification(
366 source_it);
367 if (!notification) {
368 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
369 goto end;
370 }
371
372 notification_in_range = evaluate_notification(notification,
373 &trimmer->begin, &trimmer->end);
374 if (notification_in_range) {
375 BT_MOVE(trim_it->current_notification, notification);
376 } else {
377 bt_put(notification);
378 }
379 }
380 end:
381 bt_put(source_it);
382 bt_put(component);
383 return ret;
384 }
385
386 BT_HIDDEN
387 enum bt_notification_iterator_status trimmer_iterator_seek_time(
388 struct bt_notification_iterator *iterator, int64_t time)
389 {
390 enum bt_notification_iterator_status ret;
391
392 ret = BT_NOTIFICATION_ITERATOR_STATUS_OK;
393 end:
394 return ret;
395 }
This page took 0.037213 seconds and 3 git commands to generate.