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 | ||
b4565e8b PP |
29 | #define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT" |
30 | #include "logging.h" | |
31 | ||
58a2480d | 32 | #include <babeltrace/compat/time-internal.h> |
3d2f08e7 | 33 | #include <babeltrace/compat/utc-internal.h> |
b2e0c907 PP |
34 | #include <babeltrace/graph/notification-iterator.h> |
35 | #include <babeltrace/graph/private-notification-iterator.h> | |
36 | #include <babeltrace/graph/notification.h> | |
37 | #include <babeltrace/graph/notification-event.h> | |
38 | #include <babeltrace/graph/notification-stream.h> | |
39 | #include <babeltrace/graph/notification-packet.h> | |
40 | #include <babeltrace/graph/component-filter.h> | |
41 | #include <babeltrace/graph/private-component-filter.h> | |
42 | #include <babeltrace/graph/private-port.h> | |
43 | #include <babeltrace/graph/private-connection.h> | |
44 | #include <babeltrace/graph/private-component.h> | |
73d5c1ad | 45 | #include <babeltrace/graph/connection.h> |
44d3cbf0 JG |
46 | #include <babeltrace/ctf-ir/event.h> |
47 | #include <babeltrace/ctf-ir/stream.h> | |
48 | #include <babeltrace/ctf-ir/stream-class.h> | |
ac0c6bdd | 49 | #include <babeltrace/ctf-ir/clock-class.h> |
44d3cbf0 JG |
50 | #include <babeltrace/ctf-ir/packet.h> |
51 | #include <babeltrace/ctf-ir/trace.h> | |
72208f03 | 52 | #include <babeltrace/ctf-ir/fields.h> |
44d3cbf0 | 53 | #include <assert.h> |
8b0ce102 | 54 | #include <plugins-common.h> |
44d3cbf0 | 55 | |
19ce87a4 JD |
56 | #include "trimmer.h" |
57 | #include "iterator.h" | |
58 | #include "copy.h" | |
59 | ||
86e20162 JD |
60 | static |
61 | gboolean close_packets(gpointer key, gpointer value, gpointer user_data) | |
62 | { | |
63 | struct bt_ctf_packet *writer_packet = value; | |
64 | ||
65 | bt_put(writer_packet); | |
66 | return TRUE; | |
67 | } | |
68 | ||
d3eb6e8f | 69 | BT_HIDDEN |
64cadc66 | 70 | void trimmer_iterator_finalize(struct bt_private_notification_iterator *it) |
44d3cbf0 | 71 | { |
86e20162 | 72 | struct trimmer_iterator *trim_it; |
44d3cbf0 | 73 | |
86e20162 JD |
74 | trim_it = bt_private_notification_iterator_get_user_data(it); |
75 | assert(trim_it); | |
44d3cbf0 | 76 | |
86e20162 JD |
77 | bt_put(trim_it->input_iterator); |
78 | g_hash_table_foreach_remove(trim_it->packet_map, | |
79 | close_packets, NULL); | |
80 | g_hash_table_destroy(trim_it->packet_map); | |
81 | g_free(trim_it); | |
44d3cbf0 | 82 | } |
cab3f160 JG |
83 | |
84 | BT_HIDDEN | |
d3eb6e8f | 85 | enum bt_notification_iterator_status trimmer_iterator_init( |
91457551 PP |
86 | struct bt_private_notification_iterator *iterator, |
87 | struct bt_private_port *port) | |
cab3f160 | 88 | { |
d3eb6e8f PP |
89 | enum bt_notification_iterator_status ret = |
90 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
cab3f160 | 91 | enum bt_notification_iterator_status it_ret; |
73d5c1ad | 92 | enum bt_connection_status conn_status; |
890882ef PP |
93 | struct bt_private_port *input_port = NULL; |
94 | struct bt_private_connection *connection = NULL; | |
91457551 PP |
95 | struct bt_private_component *component = |
96 | bt_private_notification_iterator_get_private_component(iterator); | |
44d3cbf0 | 97 | struct trimmer_iterator *it_data = g_new0(struct trimmer_iterator, 1); |
5fc02ebc JD |
98 | static const enum bt_notification_type notif_types[] = { |
99 | BT_NOTIFICATION_TYPE_EVENT, | |
100 | BT_NOTIFICATION_TYPE_STREAM_END, | |
101 | BT_NOTIFICATION_TYPE_PACKET_BEGIN, | |
102 | BT_NOTIFICATION_TYPE_PACKET_END, | |
103 | BT_NOTIFICATION_TYPE_SENTINEL, | |
104 | }; | |
44d3cbf0 JG |
105 | |
106 | if (!it_data) { | |
d3eb6e8f | 107 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
44d3cbf0 JG |
108 | goto end; |
109 | } | |
110 | ||
99cdb69e | 111 | /* Create a new iterator on the upstream component. */ |
b9d103be PP |
112 | input_port = bt_private_component_filter_get_input_private_port_by_name( |
113 | component, "in"); | |
99cdb69e | 114 | assert(input_port); |
890882ef | 115 | connection = bt_private_port_get_private_connection(input_port); |
99cdb69e JG |
116 | assert(connection); |
117 | ||
73d5c1ad PP |
118 | conn_status = bt_private_connection_create_notification_iterator(connection, |
119 | notif_types, &it_data->input_iterator); | |
120 | if (conn_status != BT_CONNECTION_STATUS_OK) { | |
121 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
99cdb69e JG |
122 | goto end; |
123 | } | |
124 | ||
19ce87a4 JD |
125 | it_data->err = stderr; |
126 | it_data->packet_map = g_hash_table_new_full(g_direct_hash, | |
127 | g_direct_equal, NULL, NULL); | |
128 | ||
890882ef PP |
129 | it_ret = bt_private_notification_iterator_set_user_data(iterator, |
130 | it_data); | |
44d3cbf0 JG |
131 | if (it_ret) { |
132 | goto end; | |
133 | } | |
cab3f160 | 134 | end: |
91457551 | 135 | bt_put(component); |
99cdb69e JG |
136 | bt_put(connection); |
137 | bt_put(input_port); | |
cab3f160 JG |
138 | return ret; |
139 | } | |
140 | ||
528debdf MD |
141 | static |
142 | int update_lazy_bound(struct trimmer_bound *bound, const char *name, | |
55595636 | 143 | int64_t ts, bool *lazy_update) |
528debdf MD |
144 | { |
145 | struct tm tm; | |
146 | int64_t value; | |
147 | time_t timeval; | |
148 | ||
55595636 MD |
149 | *lazy_update = false; |
150 | ||
528debdf MD |
151 | if (!bound->lazy) { |
152 | return 0; | |
153 | } | |
154 | tm.tm_isdst = -1; | |
155 | timeval = ts / NSEC_PER_SEC; | |
156 | ||
157 | if (bound->lazy_values.gmt) { | |
158 | /* Get day, month, year. */ | |
58a2480d | 159 | if (!bt_gmtime_r(&timeval, &tm)) { |
b4565e8b | 160 | BT_LOGE_STR("Failure in bt_gmtime_r()"); |
528debdf MD |
161 | goto error; |
162 | } | |
163 | tm.tm_sec = bound->lazy_values.ss; | |
164 | tm.tm_min = bound->lazy_values.mm; | |
165 | tm.tm_hour = bound->lazy_values.hh; | |
3d2f08e7 | 166 | timeval = bt_timegm(&tm); |
528debdf | 167 | if (timeval < 0) { |
b4565e8b | 168 | BT_LOGE("Failure in bt_timegm(), incorrectly formatted %s timestamp", |
55595636 | 169 | name); |
528debdf MD |
170 | goto error; |
171 | } | |
172 | } else { | |
173 | /* Get day, month, year. */ | |
58a2480d | 174 | if (!bt_localtime_r(&timeval, &tm)) { |
b4565e8b | 175 | BT_LOGE_STR("Failure in bt_localtime_r()"); |
528debdf MD |
176 | goto error; |
177 | } | |
178 | tm.tm_sec = bound->lazy_values.ss; | |
179 | tm.tm_min = bound->lazy_values.mm; | |
180 | tm.tm_hour = bound->lazy_values.hh; | |
181 | timeval = mktime(&tm); | |
182 | if (timeval < 0) { | |
b4565e8b | 183 | BT_LOGE("Failure in mktime(), incorrectly formatted %s timestamp", |
528debdf MD |
184 | name); |
185 | goto error; | |
186 | } | |
187 | } | |
188 | value = (int64_t) timeval; | |
189 | value *= NSEC_PER_SEC; | |
190 | value += bound->lazy_values.ns; | |
191 | bound->value = value; | |
192 | bound->set = true; | |
193 | bound->lazy = false; | |
55595636 | 194 | *lazy_update = true; |
528debdf MD |
195 | return 0; |
196 | ||
197 | error: | |
528debdf MD |
198 | return -1; |
199 | } | |
200 | ||
44d3cbf0 | 201 | static |
19ce87a4 JD |
202 | struct bt_notification *evaluate_event_notification( |
203 | struct bt_notification *notification, | |
204 | struct trimmer_iterator *trim_it, | |
55595636 | 205 | struct trimmer_bound *begin, struct trimmer_bound *end, |
907b1704 | 206 | bool *_event_in_range, bool *finished) |
44d3cbf0 | 207 | { |
72208f03 JG |
208 | int64_t ts; |
209 | int clock_ret; | |
19ce87a4 | 210 | struct bt_ctf_event *event = NULL, *writer_event; |
72208f03 | 211 | bool in_range = true; |
ac0c6bdd | 212 | struct bt_ctf_clock_class *clock_class = NULL; |
72208f03 | 213 | struct bt_ctf_trace *trace = NULL; |
44d3cbf0 | 214 | struct bt_ctf_stream *stream = NULL; |
72208f03 JG |
215 | struct bt_ctf_stream_class *stream_class = NULL; |
216 | struct bt_ctf_clock_value *clock_value = NULL; | |
55595636 | 217 | bool lazy_update = false; |
19ce87a4 JD |
218 | struct bt_notification *new_notification = NULL; |
219 | struct bt_clock_class_priority_map *cc_prio_map; | |
44d3cbf0 | 220 | |
72208f03 JG |
221 | event = bt_notification_event_get_event(notification); |
222 | assert(event); | |
19ce87a4 JD |
223 | cc_prio_map = bt_notification_event_get_clock_class_priority_map( |
224 | notification); | |
225 | assert(cc_prio_map); | |
226 | writer_event = trimmer_output_event(trim_it, event); | |
227 | assert(writer_event); | |
228 | new_notification = bt_notification_event_create(writer_event, cc_prio_map); | |
229 | assert(new_notification); | |
230 | bt_put(cc_prio_map); | |
44d3cbf0 | 231 | |
72208f03 JG |
232 | stream = bt_ctf_event_get_stream(event); |
233 | assert(stream); | |
44d3cbf0 | 234 | |
72208f03 JG |
235 | stream_class = bt_ctf_stream_get_class(stream); |
236 | assert(stream_class); | |
237 | ||
238 | trace = bt_ctf_stream_class_get_trace(stream_class); | |
239 | assert(trace); | |
240 | ||
241 | /* FIXME multi-clock? */ | |
9ac68eb1 | 242 | clock_class = bt_ctf_trace_get_clock_class_by_index(trace, 0); |
ac0c6bdd | 243 | if (!clock_class) { |
72208f03 | 244 | goto end; |
44d3cbf0 | 245 | } |
44d3cbf0 | 246 | |
ac0c6bdd | 247 | clock_value = bt_ctf_event_get_clock_value(event, clock_class); |
72208f03 | 248 | if (!clock_value) { |
b4565e8b | 249 | BT_LOGE_STR("Failed to retrieve clock value"); |
19ce87a4 | 250 | goto error; |
44d3cbf0 | 251 | } |
72208f03 JG |
252 | |
253 | clock_ret = bt_ctf_clock_value_get_value_ns_from_epoch( | |
254 | clock_value, &ts); | |
255 | if (clock_ret) { | |
b4565e8b | 256 | BT_LOGE_STR("Failed to retrieve clock value timestamp"); |
19ce87a4 | 257 | goto error; |
44d3cbf0 | 258 | } |
55595636 | 259 | if (update_lazy_bound(begin, "begin", ts, &lazy_update)) { |
528debdf MD |
260 | goto end; |
261 | } | |
55595636 | 262 | if (update_lazy_bound(end, "end", ts, &lazy_update)) { |
528debdf MD |
263 | goto end; |
264 | } | |
55595636 MD |
265 | if (lazy_update && begin->set && end->set) { |
266 | if (begin->value > end->value) { | |
b4565e8b | 267 | BT_LOGE_STR("Unexpected: time range begin value is above end value"); |
19ce87a4 | 268 | goto error; |
55595636 MD |
269 | } |
270 | } | |
72208f03 JG |
271 | if (begin->set && ts < begin->value) { |
272 | in_range = false; | |
273 | } | |
274 | if (end->set && ts > end->value) { | |
275 | in_range = false; | |
907b1704 | 276 | *finished = true; |
72208f03 | 277 | } |
19ce87a4 JD |
278 | |
279 | goto end; | |
280 | ||
281 | error: | |
282 | BT_PUT(new_notification); | |
44d3cbf0 | 283 | end: |
72208f03 | 284 | bt_put(event); |
19ce87a4 | 285 | bt_put(writer_event); |
ac0c6bdd | 286 | bt_put(clock_class); |
72208f03 JG |
287 | bt_put(trace); |
288 | bt_put(stream); | |
289 | bt_put(stream_class); | |
290 | bt_put(clock_value); | |
55595636 | 291 | *_event_in_range = in_range; |
19ce87a4 | 292 | return new_notification; |
44d3cbf0 JG |
293 | } |
294 | ||
44d3cbf0 | 295 | static |
72208f03 | 296 | int ns_from_integer_field(struct bt_ctf_field *integer, int64_t *ns) |
44d3cbf0 | 297 | { |
72208f03 JG |
298 | int ret = 0; |
299 | int is_signed; | |
300 | uint64_t raw_clock_value; | |
301 | struct bt_ctf_field_type *integer_type = NULL; | |
ac0c6bdd | 302 | struct bt_ctf_clock_class *clock_class = NULL; |
44d3cbf0 JG |
303 | struct bt_ctf_clock_value *clock_value = NULL; |
304 | ||
72208f03 JG |
305 | integer_type = bt_ctf_field_get_type(integer); |
306 | assert(integer_type); | |
ac0c6bdd PP |
307 | clock_class = bt_ctf_field_type_integer_get_mapped_clock_class( |
308 | integer_type); | |
309 | if (!clock_class) { | |
72208f03 | 310 | ret = -1; |
44d3cbf0 JG |
311 | goto end; |
312 | } | |
313 | ||
72208f03 JG |
314 | is_signed = bt_ctf_field_type_integer_get_signed(integer_type); |
315 | if (!is_signed) { | |
316 | ret = bt_ctf_field_unsigned_integer_get_value(integer, | |
317 | &raw_clock_value); | |
318 | if (ret) { | |
44d3cbf0 JG |
319 | goto end; |
320 | } | |
72208f03 JG |
321 | } else { |
322 | /* Signed clock values are unsupported. */ | |
a8e317ff | 323 | ret = -1; |
72208f03 JG |
324 | goto end; |
325 | } | |
44d3cbf0 | 326 | |
ac0c6bdd | 327 | clock_value = bt_ctf_clock_value_create(clock_class, raw_clock_value); |
72208f03 JG |
328 | if (!clock_value) { |
329 | goto end; | |
330 | } | |
44d3cbf0 | 331 | |
72208f03 JG |
332 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns); |
333 | end: | |
334 | bt_put(integer_type); | |
ac0c6bdd | 335 | bt_put(clock_class); |
72208f03 JG |
336 | bt_put(clock_value); |
337 | return ret; | |
338 | } | |
44d3cbf0 | 339 | |
19ce87a4 JD |
340 | static uint64_t ns_from_value(uint64_t frequency, uint64_t value) |
341 | { | |
342 | uint64_t ns; | |
343 | ||
344 | if (frequency == NSEC_PER_SEC) { | |
345 | ns = value; | |
346 | } else { | |
347 | ns = (uint64_t) ((1e9 * (double) value) / (double) frequency); | |
348 | } | |
349 | ||
350 | return ns; | |
351 | } | |
352 | ||
353 | /* | |
354 | * timestamp minus the offset. | |
355 | */ | |
72208f03 | 356 | static |
19ce87a4 JD |
357 | int64_t get_raw_timestamp(struct bt_ctf_packet *writer_packet, |
358 | int64_t timestamp) | |
359 | { | |
360 | struct bt_ctf_clock_class *writer_clock_class; | |
361 | int64_t sec_offset, cycles_offset, ns; | |
362 | struct bt_ctf_trace *writer_trace; | |
363 | struct bt_ctf_stream *writer_stream; | |
364 | struct bt_ctf_stream_class *writer_stream_class; | |
365 | int ret; | |
366 | uint64_t freq; | |
367 | ||
368 | writer_stream = bt_ctf_packet_get_stream(writer_packet); | |
369 | assert(writer_stream); | |
370 | ||
371 | writer_stream_class = bt_ctf_stream_get_class(writer_stream); | |
372 | assert(writer_stream_class); | |
373 | ||
374 | writer_trace = bt_ctf_stream_class_get_trace(writer_stream_class); | |
375 | assert(writer_trace); | |
376 | ||
377 | /* FIXME multi-clock? */ | |
9ac68eb1 PP |
378 | writer_clock_class = bt_ctf_trace_get_clock_class_by_index( |
379 | writer_trace, 0); | |
19ce87a4 JD |
380 | assert(writer_clock_class); |
381 | ||
382 | ret = bt_ctf_clock_class_get_offset_s(writer_clock_class, &sec_offset); | |
383 | assert(!ret); | |
384 | ns = sec_offset * NSEC_PER_SEC; | |
385 | ||
386 | freq = bt_ctf_clock_class_get_frequency(writer_clock_class); | |
387 | assert(freq != -1ULL); | |
388 | ||
389 | ret = bt_ctf_clock_class_get_offset_cycles(writer_clock_class, &cycles_offset); | |
390 | assert(!ret); | |
391 | ||
392 | ns += ns_from_value(freq, cycles_offset); | |
393 | ||
394 | bt_put(writer_clock_class); | |
395 | bt_put(writer_trace); | |
396 | bt_put(writer_stream_class); | |
397 | bt_put(writer_stream); | |
398 | ||
399 | return timestamp - ns; | |
400 | } | |
401 | ||
402 | static | |
403 | struct bt_notification *evaluate_packet_notification( | |
55595636 | 404 | struct bt_notification *notification, |
19ce87a4 | 405 | struct trimmer_iterator *trim_it, |
55595636 | 406 | struct trimmer_bound *begin, struct trimmer_bound *end, |
907b1704 | 407 | bool *_packet_in_range, bool *finished) |
72208f03 | 408 | { |
72208f03 JG |
409 | int64_t begin_ns, pkt_begin_ns, end_ns, pkt_end_ns; |
410 | bool in_range = true; | |
19ce87a4 | 411 | struct bt_ctf_packet *packet = NULL, *writer_packet = NULL; |
72208f03 JG |
412 | struct bt_ctf_field *packet_context = NULL, |
413 | *timestamp_begin = NULL, | |
414 | *timestamp_end = NULL; | |
19ce87a4 JD |
415 | struct bt_notification *new_notification = NULL; |
416 | enum bt_component_status ret; | |
417 | bool lazy_update = false; | |
72208f03 JG |
418 | |
419 | switch (bt_notification_get_type(notification)) { | |
420 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
421 | packet = bt_notification_packet_begin_get_packet(notification); | |
19ce87a4 JD |
422 | assert(packet); |
423 | writer_packet = trimmer_new_packet(trim_it, packet); | |
424 | assert(writer_packet); | |
44d3cbf0 | 425 | break; |
72208f03 JG |
426 | case BT_NOTIFICATION_TYPE_PACKET_END: |
427 | packet = bt_notification_packet_end_get_packet(notification); | |
19ce87a4 JD |
428 | assert(packet); |
429 | writer_packet = trimmer_close_packet(trim_it, packet); | |
430 | assert(writer_packet); | |
72208f03 JG |
431 | break; |
432 | default: | |
19ce87a4 | 433 | goto end; |
44d3cbf0 | 434 | } |
72208f03 | 435 | |
19ce87a4 | 436 | packet_context = bt_ctf_packet_get_context(writer_packet); |
72208f03 | 437 | if (!packet_context) { |
19ce87a4 | 438 | goto end_no_notif; |
72208f03 JG |
439 | } |
440 | ||
441 | if (!bt_ctf_field_is_structure(packet_context)) { | |
19ce87a4 | 442 | goto end_no_notif; |
72208f03 JG |
443 | } |
444 | ||
445 | timestamp_begin = bt_ctf_field_structure_get_field( | |
446 | packet_context, "timestamp_begin"); | |
447 | if (!timestamp_begin || !bt_ctf_field_is_integer(timestamp_begin)) { | |
19ce87a4 | 448 | goto end_no_notif; |
72208f03 JG |
449 | } |
450 | timestamp_end = bt_ctf_field_structure_get_field( | |
451 | packet_context, "timestamp_end"); | |
452 | if (!timestamp_end || !bt_ctf_field_is_integer(timestamp_end)) { | |
19ce87a4 | 453 | goto end_no_notif; |
72208f03 JG |
454 | } |
455 | ||
55595636 | 456 | if (ns_from_integer_field(timestamp_begin, &pkt_begin_ns)) { |
19ce87a4 | 457 | goto end_no_notif; |
72208f03 | 458 | } |
55595636 | 459 | if (ns_from_integer_field(timestamp_end, &pkt_end_ns)) { |
19ce87a4 JD |
460 | goto end_no_notif; |
461 | } | |
462 | ||
463 | if (update_lazy_bound(begin, "begin", pkt_begin_ns, &lazy_update)) { | |
464 | goto end_no_notif; | |
465 | } | |
466 | if (update_lazy_bound(end, "end", pkt_end_ns, &lazy_update)) { | |
467 | goto end_no_notif; | |
468 | } | |
469 | if (lazy_update && begin->set && end->set) { | |
470 | if (begin->value > end->value) { | |
b4565e8b | 471 | BT_LOGE_STR("Unexpected: time range begin value is above end value"); |
19ce87a4 JD |
472 | goto end_no_notif; |
473 | } | |
72208f03 JG |
474 | } |
475 | ||
476 | begin_ns = begin->set ? begin->value : INT64_MIN; | |
477 | end_ns = end->set ? end->value : INT64_MAX; | |
478 | ||
479 | /* | |
480 | * Accept if there is any overlap between the selected region and the | |
481 | * packet. | |
482 | */ | |
483 | in_range = (pkt_end_ns >= begin_ns) && (pkt_begin_ns <= end_ns); | |
19ce87a4 JD |
484 | if (!in_range) { |
485 | goto end_no_notif; | |
486 | } | |
907b1704 JD |
487 | if (pkt_begin_ns > end_ns) { |
488 | *finished = true; | |
489 | } | |
19ce87a4 JD |
490 | |
491 | if (begin_ns > pkt_begin_ns) { | |
492 | ret = update_packet_context_field(trim_it->err, writer_packet, | |
493 | "timestamp_begin", | |
494 | get_raw_timestamp(writer_packet, begin_ns)); | |
495 | assert(!ret); | |
496 | } | |
497 | ||
498 | if (end_ns < pkt_end_ns) { | |
499 | ret = update_packet_context_field(trim_it->err, writer_packet, | |
500 | "timestamp_end", | |
501 | get_raw_timestamp(writer_packet, end_ns)); | |
502 | assert(!ret); | |
503 | } | |
504 | ||
72208f03 | 505 | end: |
19ce87a4 JD |
506 | switch (bt_notification_get_type(notification)) { |
507 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
508 | new_notification = bt_notification_packet_begin_create(writer_packet); | |
509 | assert(new_notification); | |
510 | break; | |
511 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
512 | new_notification = bt_notification_packet_end_create(writer_packet); | |
513 | assert(new_notification); | |
514 | break; | |
515 | default: | |
516 | break; | |
517 | } | |
518 | end_no_notif: | |
55595636 | 519 | *_packet_in_range = in_range; |
72208f03 | 520 | bt_put(packet); |
19ce87a4 | 521 | bt_put(writer_packet); |
72208f03 JG |
522 | bt_put(packet_context); |
523 | bt_put(timestamp_begin); | |
524 | bt_put(timestamp_end); | |
19ce87a4 JD |
525 | return new_notification; |
526 | } | |
527 | ||
528 | static | |
529 | struct bt_notification *evaluate_stream_notification( | |
530 | struct bt_notification *notification, | |
531 | struct trimmer_iterator *trim_it) | |
532 | { | |
533 | struct bt_ctf_stream *stream; | |
534 | ||
535 | stream = bt_notification_stream_end_get_stream(notification); | |
536 | assert(stream); | |
537 | ||
538 | /* FIXME: useless copy */ | |
539 | return bt_notification_stream_end_create(stream); | |
72208f03 JG |
540 | } |
541 | ||
542 | /* Return true if the notification should be forwarded. */ | |
543 | static | |
55595636 | 544 | enum bt_notification_iterator_status evaluate_notification( |
19ce87a4 JD |
545 | struct bt_notification **notification, |
546 | struct trimmer_iterator *trim_it, | |
55595636 MD |
547 | struct trimmer_bound *begin, struct trimmer_bound *end, |
548 | bool *in_range) | |
72208f03 | 549 | { |
72208f03 | 550 | enum bt_notification_type type; |
19ce87a4 | 551 | struct bt_notification *new_notification = NULL; |
907b1704 | 552 | bool finished = false; |
72208f03 | 553 | |
b61397c4 | 554 | *in_range = true; |
19ce87a4 | 555 | type = bt_notification_get_type(*notification); |
72208f03 JG |
556 | switch (type) { |
557 | case BT_NOTIFICATION_TYPE_EVENT: | |
19ce87a4 | 558 | new_notification = evaluate_event_notification(*notification, |
907b1704 | 559 | trim_it, begin, end, in_range, &finished); |
72208f03 JG |
560 | break; |
561 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
562 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
19ce87a4 | 563 | new_notification = evaluate_packet_notification(*notification, |
907b1704 | 564 | trim_it, begin, end, in_range, &finished); |
19ce87a4 JD |
565 | break; |
566 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
567 | new_notification = evaluate_stream_notification(*notification, | |
568 | trim_it); | |
72208f03 | 569 | break; |
44d3cbf0 | 570 | default: |
5fc02ebc | 571 | puts("Unhandled notification type"); |
44d3cbf0 JG |
572 | break; |
573 | } | |
19ce87a4 JD |
574 | BT_PUT(*notification); |
575 | *notification = new_notification; | |
576 | ||
907b1704 JD |
577 | if (finished) { |
578 | return BT_NOTIFICATION_ITERATOR_STATUS_END; | |
579 | } | |
580 | ||
19ce87a4 | 581 | return BT_NOTIFICATION_ITERATOR_STATUS_OK; |
cab3f160 JG |
582 | } |
583 | ||
584 | BT_HIDDEN | |
41a2b7ae | 585 | struct bt_notification_iterator_next_return trimmer_iterator_next( |
890882ef | 586 | struct bt_private_notification_iterator *iterator) |
cab3f160 | 587 | { |
44d3cbf0 | 588 | struct trimmer_iterator *trim_it = NULL; |
890882ef | 589 | struct bt_private_component *component = NULL; |
44d3cbf0 JG |
590 | struct trimmer *trimmer = NULL; |
591 | struct bt_notification_iterator *source_it = NULL; | |
41a2b7ae PP |
592 | struct bt_notification_iterator_next_return ret = { |
593 | .status = BT_NOTIFICATION_ITERATOR_STATUS_OK, | |
594 | .notification = NULL, | |
595 | }; | |
72208f03 | 596 | bool notification_in_range = false; |
cab3f160 | 597 | |
890882ef | 598 | trim_it = bt_private_notification_iterator_get_user_data(iterator); |
44d3cbf0 JG |
599 | assert(trim_it); |
600 | ||
890882ef PP |
601 | component = bt_private_notification_iterator_get_private_component( |
602 | iterator); | |
44d3cbf0 | 603 | assert(component); |
890882ef | 604 | trimmer = bt_private_component_get_user_data(component); |
44d3cbf0 JG |
605 | assert(trimmer); |
606 | ||
99cdb69e JG |
607 | source_it = trim_it->input_iterator; |
608 | assert(source_it); | |
44d3cbf0 | 609 | |
72208f03 | 610 | while (!notification_in_range) { |
41a2b7ae PP |
611 | ret.status = bt_notification_iterator_next(source_it); |
612 | if (ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
44d3cbf0 JG |
613 | goto end; |
614 | } | |
615 | ||
41a2b7ae | 616 | ret.notification = bt_notification_iterator_get_notification( |
44d3cbf0 | 617 | source_it); |
41a2b7ae PP |
618 | if (!ret.notification) { |
619 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
44d3cbf0 JG |
620 | goto end; |
621 | } | |
622 | ||
19ce87a4 | 623 | ret.status = evaluate_notification(&ret.notification, trim_it, |
55595636 MD |
624 | &trimmer->begin, &trimmer->end, |
625 | ¬ification_in_range); | |
41a2b7ae | 626 | if (!notification_in_range) { |
19ce87a4 | 627 | BT_PUT(ret.notification); |
44d3cbf0 | 628 | } |
6d5f6792 | 629 | |
41a2b7ae | 630 | if (ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { |
6d5f6792 JG |
631 | break; |
632 | } | |
44d3cbf0 | 633 | } |
cab3f160 | 634 | end: |
44d3cbf0 | 635 | bt_put(component); |
cab3f160 JG |
636 | return ret; |
637 | } | |
638 | ||
639 | BT_HIDDEN | |
640 | enum bt_notification_iterator_status trimmer_iterator_seek_time( | |
890882ef PP |
641 | struct bt_private_notification_iterator *iterator, |
642 | int64_t time) | |
cab3f160 | 643 | { |
72b913fb | 644 | return BT_NOTIFICATION_ITERATOR_STATUS_OK; |
cab3f160 | 645 | } |