Commit | Line | Data |
---|---|---|
cab3f160 JG |
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> | |
44d3cbf0 JG |
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> | |
72208f03 | 43 | #include <babeltrace/ctf-ir/fields.h> |
44d3cbf0 JG |
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); | |
44d3cbf0 JG |
58 | g_free(it_data); |
59 | } | |
cab3f160 JG |
60 | |
61 | BT_HIDDEN | |
62 | enum bt_component_status trimmer_iterator_init(struct bt_component *component, | |
63 | struct bt_notification_iterator *iterator) | |
64 | { | |
44d3cbf0 | 65 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
cab3f160 | 66 | enum bt_notification_iterator_status it_ret; |
44d3cbf0 JG |
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 | } | |
cab3f160 JG |
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 | { | |
44d3cbf0 JG |
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 | ||
528debdf MD |
132 | static |
133 | int update_lazy_bound(struct trimmer_bound *bound, const char *name, | |
134 | int64_t ts) | |
135 | { | |
136 | struct tm tm; | |
137 | int64_t value; | |
138 | time_t timeval; | |
139 | ||
140 | if (!bound->lazy) { | |
141 | return 0; | |
142 | } | |
143 | tm.tm_isdst = -1; | |
144 | timeval = ts / NSEC_PER_SEC; | |
145 | ||
146 | if (bound->lazy_values.gmt) { | |
147 | /* Get day, month, year. */ | |
148 | if (!gmtime_r(&timeval, &tm)) { | |
149 | printf_error("Failure in gmtime_r()\n"); | |
150 | goto error; | |
151 | } | |
152 | tm.tm_sec = bound->lazy_values.ss; | |
153 | tm.tm_min = bound->lazy_values.mm; | |
154 | tm.tm_hour = bound->lazy_values.hh; | |
155 | timeval = timegm(&tm); | |
156 | if (timeval < 0) { | |
157 | printf_error("Failure in timegm(), incorrectly formatted %s timestamp\n", | |
158 | name); | |
159 | goto error; | |
160 | } | |
161 | } else { | |
162 | /* Get day, month, year. */ | |
163 | if (!localtime_r(&timeval, &tm)) { | |
164 | printf_error("Failure in localtime_r()\n"); | |
165 | goto error; | |
166 | } | |
167 | tm.tm_sec = bound->lazy_values.ss; | |
168 | tm.tm_min = bound->lazy_values.mm; | |
169 | tm.tm_hour = bound->lazy_values.hh; | |
170 | timeval = mktime(&tm); | |
171 | if (timeval < 0) { | |
172 | printf_error("Failure in mktime(), incorrectly formatted %s timestamp\n", | |
173 | name); | |
174 | goto error; | |
175 | } | |
176 | } | |
177 | value = (int64_t) timeval; | |
178 | value *= NSEC_PER_SEC; | |
179 | value += bound->lazy_values.ns; | |
180 | bound->value = value; | |
181 | bound->set = true; | |
182 | bound->lazy = false; | |
183 | return 0; | |
184 | ||
185 | error: | |
186 | bound->lazy = false; | |
187 | return -1; | |
188 | } | |
189 | ||
44d3cbf0 | 190 | static |
72208f03 JG |
191 | bool evaluate_event_notification(struct bt_notification *notification, |
192 | struct trimmer_bound *begin, struct trimmer_bound *end) | |
44d3cbf0 | 193 | { |
72208f03 JG |
194 | int64_t ts; |
195 | int clock_ret; | |
196 | struct bt_ctf_event *event = NULL; | |
197 | bool in_range = true; | |
198 | struct bt_ctf_clock *clock = NULL; | |
199 | struct bt_ctf_trace *trace = NULL; | |
44d3cbf0 | 200 | struct bt_ctf_stream *stream = NULL; |
72208f03 JG |
201 | struct bt_ctf_stream_class *stream_class = NULL; |
202 | struct bt_ctf_clock_value *clock_value = NULL; | |
44d3cbf0 | 203 | |
72208f03 JG |
204 | event = bt_notification_event_get_event(notification); |
205 | assert(event); | |
44d3cbf0 | 206 | |
72208f03 JG |
207 | stream = bt_ctf_event_get_stream(event); |
208 | assert(stream); | |
44d3cbf0 | 209 | |
72208f03 JG |
210 | stream_class = bt_ctf_stream_get_class(stream); |
211 | assert(stream_class); | |
212 | ||
213 | trace = bt_ctf_stream_class_get_trace(stream_class); | |
214 | assert(trace); | |
215 | ||
216 | /* FIXME multi-clock? */ | |
217 | clock = bt_ctf_trace_get_clock(trace, 0); | |
218 | if (!clock) { | |
219 | goto end; | |
44d3cbf0 | 220 | } |
44d3cbf0 | 221 | |
72208f03 JG |
222 | clock_value = bt_ctf_event_get_clock_value(event, clock); |
223 | if (!clock_value) { | |
224 | printf_error("Failed to retrieve clock value\n"); | |
225 | goto end; | |
44d3cbf0 | 226 | } |
72208f03 JG |
227 | |
228 | clock_ret = bt_ctf_clock_value_get_value_ns_from_epoch( | |
229 | clock_value, &ts); | |
230 | if (clock_ret) { | |
231 | printf_error("Failed to retrieve clock value timestamp\n"); | |
44d3cbf0 JG |
232 | goto end; |
233 | } | |
528debdf MD |
234 | if (update_lazy_bound(begin, "begin", ts)) { |
235 | goto end; | |
236 | } | |
237 | if (update_lazy_bound(end, "end", ts)) { | |
238 | goto end; | |
239 | } | |
72208f03 JG |
240 | if (begin->set && ts < begin->value) { |
241 | in_range = false; | |
242 | } | |
243 | if (end->set && ts > end->value) { | |
244 | in_range = false; | |
245 | } | |
44d3cbf0 | 246 | end: |
72208f03 JG |
247 | bt_put(event); |
248 | bt_put(clock); | |
249 | bt_put(trace); | |
250 | bt_put(stream); | |
251 | bt_put(stream_class); | |
252 | bt_put(clock_value); | |
253 | return in_range; | |
44d3cbf0 JG |
254 | } |
255 | ||
44d3cbf0 | 256 | static |
72208f03 | 257 | int ns_from_integer_field(struct bt_ctf_field *integer, int64_t *ns) |
44d3cbf0 | 258 | { |
72208f03 JG |
259 | int ret = 0; |
260 | int is_signed; | |
261 | uint64_t raw_clock_value; | |
262 | struct bt_ctf_field_type *integer_type = NULL; | |
44d3cbf0 | 263 | struct bt_ctf_clock *clock = NULL; |
44d3cbf0 JG |
264 | struct bt_ctf_clock_value *clock_value = NULL; |
265 | ||
72208f03 JG |
266 | integer_type = bt_ctf_field_get_type(integer); |
267 | assert(integer_type); | |
268 | clock = bt_ctf_field_type_integer_get_mapped_clock(integer_type); | |
269 | if (!clock) { | |
270 | ret = -1; | |
44d3cbf0 JG |
271 | goto end; |
272 | } | |
273 | ||
72208f03 JG |
274 | is_signed = bt_ctf_field_type_integer_get_signed(integer_type); |
275 | if (!is_signed) { | |
276 | ret = bt_ctf_field_unsigned_integer_get_value(integer, | |
277 | &raw_clock_value); | |
278 | if (ret) { | |
44d3cbf0 JG |
279 | goto end; |
280 | } | |
72208f03 JG |
281 | } else { |
282 | /* Signed clock values are unsupported. */ | |
283 | goto end; | |
284 | } | |
44d3cbf0 | 285 | |
72208f03 JG |
286 | clock_value = bt_ctf_clock_value_create(clock, raw_clock_value); |
287 | if (!clock_value) { | |
288 | goto end; | |
289 | } | |
44d3cbf0 | 290 | |
72208f03 JG |
291 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns); |
292 | end: | |
293 | bt_put(integer_type); | |
294 | bt_put(clock); | |
295 | bt_put(clock_value); | |
296 | return ret; | |
297 | } | |
44d3cbf0 | 298 | |
72208f03 JG |
299 | static |
300 | bool evaluate_packet_notification(struct bt_notification *notification, | |
301 | struct trimmer_bound *begin, struct trimmer_bound *end) | |
302 | { | |
303 | int ret; | |
304 | int64_t begin_ns, pkt_begin_ns, end_ns, pkt_end_ns; | |
305 | bool in_range = true; | |
306 | struct bt_ctf_packet *packet = NULL; | |
307 | struct bt_ctf_field *packet_context = NULL, | |
308 | *timestamp_begin = NULL, | |
309 | *timestamp_end = NULL; | |
310 | ||
311 | switch (bt_notification_get_type(notification)) { | |
312 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
313 | packet = bt_notification_packet_begin_get_packet(notification); | |
44d3cbf0 | 314 | break; |
72208f03 JG |
315 | case BT_NOTIFICATION_TYPE_PACKET_END: |
316 | packet = bt_notification_packet_end_get_packet(notification); | |
317 | break; | |
318 | default: | |
319 | abort(); | |
44d3cbf0 | 320 | } |
72208f03 JG |
321 | assert(packet); |
322 | ||
323 | packet_context = bt_ctf_packet_get_context(packet); | |
324 | if (!packet_context) { | |
325 | goto end; | |
326 | } | |
327 | ||
328 | if (!bt_ctf_field_is_structure(packet_context)) { | |
329 | goto end; | |
330 | } | |
331 | ||
332 | timestamp_begin = bt_ctf_field_structure_get_field( | |
333 | packet_context, "timestamp_begin"); | |
334 | if (!timestamp_begin || !bt_ctf_field_is_integer(timestamp_begin)) { | |
335 | goto end; | |
336 | } | |
337 | timestamp_end = bt_ctf_field_structure_get_field( | |
338 | packet_context, "timestamp_end"); | |
339 | if (!timestamp_end || !bt_ctf_field_is_integer(timestamp_end)) { | |
340 | goto end; | |
341 | } | |
342 | ||
343 | ret = ns_from_integer_field(timestamp_begin, &pkt_begin_ns); | |
344 | if (ret) { | |
345 | goto end; | |
346 | } | |
347 | ret = ns_from_integer_field(timestamp_end, &pkt_end_ns); | |
348 | if (ret) { | |
349 | goto end; | |
350 | } | |
351 | ||
352 | begin_ns = begin->set ? begin->value : INT64_MIN; | |
353 | end_ns = end->set ? end->value : INT64_MAX; | |
354 | ||
355 | /* | |
356 | * Accept if there is any overlap between the selected region and the | |
357 | * packet. | |
358 | */ | |
359 | in_range = (pkt_end_ns >= begin_ns) && (pkt_begin_ns <= end_ns); | |
360 | end: | |
361 | bt_put(packet); | |
362 | bt_put(packet_context); | |
363 | bt_put(timestamp_begin); | |
364 | bt_put(timestamp_end); | |
365 | return in_range; | |
366 | } | |
367 | ||
368 | /* Return true if the notification should be forwarded. */ | |
369 | static | |
370 | bool evaluate_notification(struct bt_notification *notification, | |
371 | struct trimmer_bound *begin, struct trimmer_bound *end) | |
372 | { | |
373 | bool in_range = true; | |
374 | enum bt_notification_type type; | |
375 | ||
376 | type = bt_notification_get_type(notification); | |
377 | switch (type) { | |
378 | case BT_NOTIFICATION_TYPE_EVENT: | |
379 | in_range = evaluate_event_notification(notification, begin, | |
380 | end); | |
381 | break; | |
382 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
383 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
384 | in_range = evaluate_packet_notification(notification, begin, | |
385 | end); | |
386 | break; | |
44d3cbf0 | 387 | default: |
72208f03 | 388 | /* Accept all other notifications. */ |
44d3cbf0 JG |
389 | break; |
390 | } | |
72208f03 | 391 | return in_range; |
cab3f160 JG |
392 | } |
393 | ||
394 | BT_HIDDEN | |
395 | enum bt_notification_iterator_status trimmer_iterator_next( | |
396 | struct bt_notification_iterator *iterator) | |
397 | { | |
44d3cbf0 JG |
398 | struct trimmer_iterator *trim_it = NULL; |
399 | struct bt_component *component = NULL; | |
400 | struct trimmer *trimmer = NULL; | |
401 | struct bt_notification_iterator *source_it = NULL; | |
402 | enum bt_notification_iterator_status ret = | |
403 | BT_NOTIFICATION_ITERATOR_STATUS_OK;; | |
404 | enum bt_component_status component_ret; | |
72208f03 | 405 | bool notification_in_range = false; |
cab3f160 | 406 | |
44d3cbf0 JG |
407 | trim_it = bt_notification_iterator_get_private_data(iterator); |
408 | assert(trim_it); | |
409 | ||
410 | component = bt_notification_iterator_get_component(iterator); | |
411 | assert(component); | |
412 | trimmer = bt_component_get_private_data(component); | |
413 | assert(trimmer); | |
414 | ||
415 | /* FIXME, should handle input iterator groups. */ | |
416 | component_ret = bt_component_filter_get_input_iterator(component, 0, | |
417 | &source_it); | |
418 | assert((component_ret == BT_COMPONENT_STATUS_OK) && source_it); | |
419 | ||
72208f03 | 420 | while (!notification_in_range) { |
44d3cbf0 JG |
421 | struct bt_notification *notification; |
422 | ||
423 | ret = bt_notification_iterator_next(source_it); | |
424 | if (ret != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
425 | goto end; | |
426 | } | |
427 | ||
428 | notification = bt_notification_iterator_get_notification( | |
429 | source_it); | |
430 | if (!notification) { | |
431 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
432 | goto end; | |
433 | } | |
434 | ||
72208f03 JG |
435 | notification_in_range = evaluate_notification(notification, |
436 | &trimmer->begin, &trimmer->end); | |
437 | if (notification_in_range) { | |
44d3cbf0 JG |
438 | BT_MOVE(trim_it->current_notification, notification); |
439 | } else { | |
440 | bt_put(notification); | |
441 | } | |
442 | } | |
cab3f160 | 443 | end: |
44d3cbf0 JG |
444 | bt_put(source_it); |
445 | bt_put(component); | |
cab3f160 JG |
446 | return ret; |
447 | } | |
448 | ||
449 | BT_HIDDEN | |
450 | enum bt_notification_iterator_status trimmer_iterator_seek_time( | |
451 | struct bt_notification_iterator *iterator, int64_t time) | |
452 | { | |
453 | enum bt_notification_iterator_status ret; | |
454 | ||
455 | ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
456 | end: | |
457 | return ret; | |
458 | } |