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