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