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