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" | |
33b34c43 | 31 | #include <babeltrace/component/notification/iterator.h> |
890882ef | 32 | #include <babeltrace/component/notification/private-iterator.h> |
33b34c43 PP |
33 | #include <babeltrace/component/notification/notification.h> |
34 | #include <babeltrace/component/notification/event.h> | |
35 | #include <babeltrace/component/notification/stream.h> | |
36 | #include <babeltrace/component/notification/packet.h> | |
d71dcf2c | 37 | #include <babeltrace/component/component-filter.h> |
890882ef PP |
38 | #include <babeltrace/component/private-component-filter.h> |
39 | #include <babeltrace/component/private-port.h> | |
40 | #include <babeltrace/component/private-connection.h> | |
41 | #include <babeltrace/component/private-component.h> | |
44d3cbf0 JG |
42 | #include <babeltrace/ctf-ir/event.h> |
43 | #include <babeltrace/ctf-ir/stream.h> | |
44 | #include <babeltrace/ctf-ir/stream-class.h> | |
ac0c6bdd | 45 | #include <babeltrace/ctf-ir/clock-class.h> |
44d3cbf0 JG |
46 | #include <babeltrace/ctf-ir/packet.h> |
47 | #include <babeltrace/ctf-ir/trace.h> | |
72208f03 | 48 | #include <babeltrace/ctf-ir/fields.h> |
44d3cbf0 | 49 | #include <assert.h> |
8b0ce102 | 50 | #include <plugins-common.h> |
44d3cbf0 | 51 | |
d3eb6e8f | 52 | BT_HIDDEN |
64cadc66 | 53 | void trimmer_iterator_finalize(struct bt_private_notification_iterator *it) |
44d3cbf0 JG |
54 | { |
55 | struct trimmer_iterator *it_data; | |
56 | ||
890882ef | 57 | it_data = bt_private_notification_iterator_get_user_data(it); |
44d3cbf0 JG |
58 | assert(it_data); |
59 | ||
99cdb69e | 60 | bt_put(it_data->input_iterator); |
44d3cbf0 JG |
61 | g_free(it_data); |
62 | } | |
cab3f160 JG |
63 | |
64 | BT_HIDDEN | |
d3eb6e8f | 65 | enum bt_notification_iterator_status trimmer_iterator_init( |
91457551 PP |
66 | struct bt_private_notification_iterator *iterator, |
67 | struct bt_private_port *port) | |
cab3f160 | 68 | { |
d3eb6e8f PP |
69 | enum bt_notification_iterator_status ret = |
70 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
cab3f160 | 71 | enum bt_notification_iterator_status it_ret; |
890882ef PP |
72 | struct bt_private_port *input_port = NULL; |
73 | struct bt_private_connection *connection = NULL; | |
91457551 PP |
74 | struct bt_private_component *component = |
75 | bt_private_notification_iterator_get_private_component(iterator); | |
44d3cbf0 JG |
76 | struct trimmer_iterator *it_data = g_new0(struct trimmer_iterator, 1); |
77 | ||
78 | if (!it_data) { | |
d3eb6e8f | 79 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
44d3cbf0 JG |
80 | goto end; |
81 | } | |
82 | ||
99cdb69e | 83 | /* Create a new iterator on the upstream component. */ |
890882ef PP |
84 | input_port = bt_private_component_filter_get_default_input_private_port( |
85 | component); | |
99cdb69e | 86 | assert(input_port); |
890882ef | 87 | connection = bt_private_port_get_private_connection(input_port); |
99cdb69e JG |
88 | assert(connection); |
89 | ||
890882ef PP |
90 | it_data->input_iterator = |
91 | bt_private_connection_create_notification_iterator(connection); | |
99cdb69e JG |
92 | if (!it_data->input_iterator) { |
93 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; | |
94 | goto end; | |
95 | } | |
96 | ||
890882ef PP |
97 | it_ret = bt_private_notification_iterator_set_user_data(iterator, |
98 | it_data); | |
44d3cbf0 JG |
99 | if (it_ret) { |
100 | goto end; | |
101 | } | |
cab3f160 | 102 | end: |
91457551 | 103 | bt_put(component); |
99cdb69e JG |
104 | bt_put(connection); |
105 | bt_put(input_port); | |
cab3f160 JG |
106 | return ret; |
107 | } | |
108 | ||
528debdf MD |
109 | static |
110 | int update_lazy_bound(struct trimmer_bound *bound, const char *name, | |
55595636 | 111 | int64_t ts, bool *lazy_update) |
528debdf MD |
112 | { |
113 | struct tm tm; | |
114 | int64_t value; | |
115 | time_t timeval; | |
116 | ||
55595636 MD |
117 | *lazy_update = false; |
118 | ||
528debdf MD |
119 | if (!bound->lazy) { |
120 | return 0; | |
121 | } | |
122 | tm.tm_isdst = -1; | |
123 | timeval = ts / NSEC_PER_SEC; | |
124 | ||
125 | if (bound->lazy_values.gmt) { | |
126 | /* Get day, month, year. */ | |
127 | if (!gmtime_r(&timeval, &tm)) { | |
55595636 | 128 | printf_error("Failure in gmtime_r()"); |
528debdf MD |
129 | goto error; |
130 | } | |
131 | tm.tm_sec = bound->lazy_values.ss; | |
132 | tm.tm_min = bound->lazy_values.mm; | |
133 | tm.tm_hour = bound->lazy_values.hh; | |
134 | timeval = timegm(&tm); | |
135 | if (timeval < 0) { | |
55595636 MD |
136 | printf_error("Failure in timegm(), incorrectly formatted %s timestamp", |
137 | name); | |
528debdf MD |
138 | goto error; |
139 | } | |
140 | } else { | |
141 | /* Get day, month, year. */ | |
142 | if (!localtime_r(&timeval, &tm)) { | |
55595636 | 143 | printf_error("Failure in localtime_r()"); |
528debdf MD |
144 | goto error; |
145 | } | |
146 | tm.tm_sec = bound->lazy_values.ss; | |
147 | tm.tm_min = bound->lazy_values.mm; | |
148 | tm.tm_hour = bound->lazy_values.hh; | |
149 | timeval = mktime(&tm); | |
150 | if (timeval < 0) { | |
55595636 | 151 | printf_error("Failure in mktime(), incorrectly formatted %s timestamp", |
528debdf MD |
152 | name); |
153 | goto error; | |
154 | } | |
155 | } | |
156 | value = (int64_t) timeval; | |
157 | value *= NSEC_PER_SEC; | |
158 | value += bound->lazy_values.ns; | |
159 | bound->value = value; | |
160 | bound->set = true; | |
161 | bound->lazy = false; | |
55595636 | 162 | *lazy_update = true; |
528debdf MD |
163 | return 0; |
164 | ||
165 | error: | |
528debdf MD |
166 | return -1; |
167 | } | |
168 | ||
44d3cbf0 | 169 | static |
55595636 MD |
170 | enum bt_notification_iterator_status |
171 | evaluate_event_notification(struct bt_notification *notification, | |
172 | struct trimmer_bound *begin, struct trimmer_bound *end, | |
173 | bool *_event_in_range) | |
44d3cbf0 | 174 | { |
72208f03 JG |
175 | int64_t ts; |
176 | int clock_ret; | |
177 | struct bt_ctf_event *event = NULL; | |
178 | bool in_range = true; | |
ac0c6bdd | 179 | struct bt_ctf_clock_class *clock_class = NULL; |
72208f03 | 180 | struct bt_ctf_trace *trace = NULL; |
44d3cbf0 | 181 | struct bt_ctf_stream *stream = NULL; |
72208f03 JG |
182 | struct bt_ctf_stream_class *stream_class = NULL; |
183 | struct bt_ctf_clock_value *clock_value = NULL; | |
55595636 MD |
184 | enum bt_notification_iterator_status ret = |
185 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
186 | bool lazy_update = false; | |
44d3cbf0 | 187 | |
72208f03 JG |
188 | event = bt_notification_event_get_event(notification); |
189 | assert(event); | |
44d3cbf0 | 190 | |
72208f03 JG |
191 | stream = bt_ctf_event_get_stream(event); |
192 | assert(stream); | |
44d3cbf0 | 193 | |
72208f03 JG |
194 | stream_class = bt_ctf_stream_get_class(stream); |
195 | assert(stream_class); | |
196 | ||
197 | trace = bt_ctf_stream_class_get_trace(stream_class); | |
198 | assert(trace); | |
199 | ||
200 | /* FIXME multi-clock? */ | |
ac0c6bdd PP |
201 | clock_class = bt_ctf_trace_get_clock_class(trace, 0); |
202 | if (!clock_class) { | |
72208f03 | 203 | goto end; |
44d3cbf0 | 204 | } |
44d3cbf0 | 205 | |
ac0c6bdd | 206 | clock_value = bt_ctf_event_get_clock_value(event, clock_class); |
72208f03 | 207 | if (!clock_value) { |
55595636 MD |
208 | printf_error("Failed to retrieve clock value"); |
209 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
72208f03 | 210 | goto end; |
44d3cbf0 | 211 | } |
72208f03 JG |
212 | |
213 | clock_ret = bt_ctf_clock_value_get_value_ns_from_epoch( | |
214 | clock_value, &ts); | |
215 | if (clock_ret) { | |
55595636 MD |
216 | printf_error("Failed to retrieve clock value timestamp"); |
217 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
44d3cbf0 JG |
218 | goto end; |
219 | } | |
55595636 MD |
220 | if (update_lazy_bound(begin, "begin", ts, &lazy_update)) { |
221 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
528debdf MD |
222 | goto end; |
223 | } | |
55595636 MD |
224 | if (update_lazy_bound(end, "end", ts, &lazy_update)) { |
225 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
528debdf MD |
226 | goto end; |
227 | } | |
55595636 MD |
228 | if (lazy_update && begin->set && end->set) { |
229 | if (begin->value > end->value) { | |
230 | printf_error("Unexpected: time range begin value is above end value"); | |
231 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
232 | goto end; | |
233 | } | |
234 | } | |
72208f03 JG |
235 | if (begin->set && ts < begin->value) { |
236 | in_range = false; | |
237 | } | |
238 | if (end->set && ts > end->value) { | |
239 | in_range = false; | |
240 | } | |
44d3cbf0 | 241 | end: |
72208f03 | 242 | bt_put(event); |
ac0c6bdd | 243 | bt_put(clock_class); |
72208f03 JG |
244 | bt_put(trace); |
245 | bt_put(stream); | |
246 | bt_put(stream_class); | |
247 | bt_put(clock_value); | |
55595636 MD |
248 | *_event_in_range = in_range; |
249 | return ret; | |
44d3cbf0 JG |
250 | } |
251 | ||
44d3cbf0 | 252 | static |
72208f03 | 253 | int ns_from_integer_field(struct bt_ctf_field *integer, int64_t *ns) |
44d3cbf0 | 254 | { |
72208f03 JG |
255 | int ret = 0; |
256 | int is_signed; | |
257 | uint64_t raw_clock_value; | |
258 | struct bt_ctf_field_type *integer_type = NULL; | |
ac0c6bdd | 259 | struct bt_ctf_clock_class *clock_class = NULL; |
44d3cbf0 JG |
260 | struct bt_ctf_clock_value *clock_value = NULL; |
261 | ||
72208f03 JG |
262 | integer_type = bt_ctf_field_get_type(integer); |
263 | assert(integer_type); | |
ac0c6bdd PP |
264 | clock_class = bt_ctf_field_type_integer_get_mapped_clock_class( |
265 | integer_type); | |
266 | if (!clock_class) { | |
72208f03 | 267 | ret = -1; |
44d3cbf0 JG |
268 | goto end; |
269 | } | |
270 | ||
72208f03 JG |
271 | is_signed = bt_ctf_field_type_integer_get_signed(integer_type); |
272 | if (!is_signed) { | |
273 | ret = bt_ctf_field_unsigned_integer_get_value(integer, | |
274 | &raw_clock_value); | |
275 | if (ret) { | |
44d3cbf0 JG |
276 | goto end; |
277 | } | |
72208f03 JG |
278 | } else { |
279 | /* Signed clock values are unsupported. */ | |
280 | goto end; | |
281 | } | |
44d3cbf0 | 282 | |
ac0c6bdd | 283 | clock_value = bt_ctf_clock_value_create(clock_class, raw_clock_value); |
72208f03 JG |
284 | if (!clock_value) { |
285 | goto end; | |
286 | } | |
44d3cbf0 | 287 | |
72208f03 JG |
288 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns); |
289 | end: | |
290 | bt_put(integer_type); | |
ac0c6bdd | 291 | bt_put(clock_class); |
72208f03 JG |
292 | bt_put(clock_value); |
293 | return ret; | |
294 | } | |
44d3cbf0 | 295 | |
72208f03 | 296 | static |
55595636 MD |
297 | enum bt_notification_iterator_status evaluate_packet_notification( |
298 | struct bt_notification *notification, | |
299 | struct trimmer_bound *begin, struct trimmer_bound *end, | |
300 | bool *_packet_in_range) | |
72208f03 | 301 | { |
55595636 MD |
302 | enum bt_notification_iterator_status ret = |
303 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
72208f03 JG |
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: | |
55595636 | 319 | break; |
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 | ||
55595636 | 343 | if (ns_from_integer_field(timestamp_begin, &pkt_begin_ns)) { |
72208f03 JG |
344 | goto end; |
345 | } | |
55595636 | 346 | if (ns_from_integer_field(timestamp_end, &pkt_end_ns)) { |
72208f03 JG |
347 | goto end; |
348 | } | |
349 | ||
350 | begin_ns = begin->set ? begin->value : INT64_MIN; | |
351 | end_ns = end->set ? end->value : INT64_MAX; | |
352 | ||
353 | /* | |
354 | * Accept if there is any overlap between the selected region and the | |
355 | * packet. | |
356 | */ | |
357 | in_range = (pkt_end_ns >= begin_ns) && (pkt_begin_ns <= end_ns); | |
358 | end: | |
55595636 | 359 | *_packet_in_range = in_range; |
72208f03 JG |
360 | bt_put(packet); |
361 | bt_put(packet_context); | |
362 | bt_put(timestamp_begin); | |
363 | bt_put(timestamp_end); | |
55595636 | 364 | return ret; |
72208f03 JG |
365 | } |
366 | ||
367 | /* Return true if the notification should be forwarded. */ | |
368 | static | |
55595636 MD |
369 | enum bt_notification_iterator_status evaluate_notification( |
370 | struct bt_notification *notification, | |
371 | struct trimmer_bound *begin, struct trimmer_bound *end, | |
372 | bool *in_range) | |
72208f03 | 373 | { |
72208f03 | 374 | enum bt_notification_type type; |
55595636 MD |
375 | enum bt_notification_iterator_status ret = |
376 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
72208f03 | 377 | |
b61397c4 | 378 | *in_range = true; |
72208f03 JG |
379 | type = bt_notification_get_type(notification); |
380 | switch (type) { | |
381 | case BT_NOTIFICATION_TYPE_EVENT: | |
55595636 MD |
382 | ret = evaluate_event_notification(notification, begin, |
383 | end, in_range); | |
72208f03 JG |
384 | break; |
385 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
386 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
55595636 MD |
387 | ret = evaluate_packet_notification(notification, begin, |
388 | end, in_range); | |
72208f03 | 389 | break; |
44d3cbf0 | 390 | default: |
72208f03 | 391 | /* Accept all other notifications. */ |
44d3cbf0 JG |
392 | break; |
393 | } | |
55595636 | 394 | return ret; |
cab3f160 JG |
395 | } |
396 | ||
397 | BT_HIDDEN | |
41a2b7ae | 398 | struct bt_notification_iterator_next_return trimmer_iterator_next( |
890882ef | 399 | struct bt_private_notification_iterator *iterator) |
cab3f160 | 400 | { |
44d3cbf0 | 401 | struct trimmer_iterator *trim_it = NULL; |
890882ef | 402 | struct bt_private_component *component = NULL; |
44d3cbf0 JG |
403 | struct trimmer *trimmer = NULL; |
404 | struct bt_notification_iterator *source_it = NULL; | |
41a2b7ae PP |
405 | struct bt_notification_iterator_next_return ret = { |
406 | .status = BT_NOTIFICATION_ITERATOR_STATUS_OK, | |
407 | .notification = NULL, | |
408 | }; | |
72208f03 | 409 | bool notification_in_range = false; |
cab3f160 | 410 | |
890882ef | 411 | trim_it = bt_private_notification_iterator_get_user_data(iterator); |
44d3cbf0 JG |
412 | assert(trim_it); |
413 | ||
890882ef PP |
414 | component = bt_private_notification_iterator_get_private_component( |
415 | iterator); | |
44d3cbf0 | 416 | assert(component); |
890882ef | 417 | trimmer = bt_private_component_get_user_data(component); |
44d3cbf0 JG |
418 | assert(trimmer); |
419 | ||
99cdb69e JG |
420 | source_it = trim_it->input_iterator; |
421 | assert(source_it); | |
44d3cbf0 | 422 | |
72208f03 | 423 | while (!notification_in_range) { |
41a2b7ae PP |
424 | ret.status = bt_notification_iterator_next(source_it); |
425 | if (ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
44d3cbf0 JG |
426 | goto end; |
427 | } | |
428 | ||
41a2b7ae | 429 | ret.notification = bt_notification_iterator_get_notification( |
44d3cbf0 | 430 | source_it); |
41a2b7ae PP |
431 | if (!ret.notification) { |
432 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
44d3cbf0 JG |
433 | goto end; |
434 | } | |
435 | ||
41a2b7ae | 436 | ret.status = evaluate_notification(ret.notification, |
55595636 MD |
437 | &trimmer->begin, &trimmer->end, |
438 | ¬ification_in_range); | |
41a2b7ae PP |
439 | if (!notification_in_range) { |
440 | bt_put(ret.notification); | |
44d3cbf0 | 441 | } |
6d5f6792 | 442 | |
41a2b7ae | 443 | if (ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { |
6d5f6792 JG |
444 | break; |
445 | } | |
44d3cbf0 | 446 | } |
cab3f160 | 447 | end: |
44d3cbf0 | 448 | bt_put(component); |
cab3f160 JG |
449 | return ret; |
450 | } | |
451 | ||
452 | BT_HIDDEN | |
453 | enum bt_notification_iterator_status trimmer_iterator_seek_time( | |
890882ef PP |
454 | struct bt_private_notification_iterator *iterator, |
455 | int64_t time) | |
cab3f160 | 456 | { |
72b913fb | 457 | return BT_NOTIFICATION_ITERATOR_STATUS_OK; |
cab3f160 | 458 | } |