4 * Babeltrace Trace Trimmer Iterator
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT-ITER"
32 #include <babeltrace/compat/time-internal.h>
33 #include <babeltrace/compat/utc-internal.h>
34 #include <babeltrace/babeltrace.h>
35 #include <babeltrace/assert-internal.h>
36 #include <plugins-common.h>
43 gboolean
close_packets(gpointer key
, gpointer value
, gpointer user_data
)
45 const struct bt_packet
*writer_packet
= value
;
47 bt_object_put_ref(writer_packet
);
52 void trimmer_iterator_finalize(struct bt_self_notification_iterator
*it
)
54 struct trimmer_iterator
*trim_it
;
56 trim_it
= bt_self_notification_iterator_get_user_data(it
);
59 bt_object_put_ref(trim_it
->input_iterator
);
60 g_hash_table_foreach_remove(trim_it
->packet_map
,
62 g_hash_table_destroy(trim_it
->packet_map
);
67 enum bt_notification_iterator_status
trimmer_iterator_init(
68 struct bt_self_notification_iterator
*iterator
,
69 struct bt_private_port
*port
)
71 enum bt_notification_iterator_status ret
=
72 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
73 enum bt_notification_iterator_status it_ret
;
74 enum bt_connection_status conn_status
;
75 struct bt_private_port
*input_port
= NULL
;
76 struct bt_private_connection
*connection
= NULL
;
77 struct bt_self_component
*component
=
78 bt_self_notification_iterator_get_private_component(iterator
);
79 struct trimmer_iterator
*it_data
= g_new0(struct trimmer_iterator
, 1);
82 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
86 /* Create a new iterator on the upstream component. */
87 input_port
= bt_self_component_filter_get_input_port_by_name(
89 BT_ASSERT(input_port
);
90 connection
= bt_private_port_get_connection(input_port
);
91 BT_ASSERT(connection
);
93 conn_status
= bt_private_connection_create_notification_iterator(connection
,
94 &it_data
->input_iterator
);
95 if (conn_status
!= BT_CONNECTION_STATUS_OK
) {
96 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
100 it_data
->err
= stderr
;
101 it_data
->packet_map
= g_hash_table_new_full(g_direct_hash
,
102 g_direct_equal
, NULL
, NULL
);
104 it_ret
= bt_self_notification_iterator_set_user_data(iterator
,
110 bt_object_put_ref(component
);
111 bt_object_put_ref(connection
);
112 bt_object_put_ref(input_port
);
117 int update_lazy_bound(struct trimmer_bound
*bound
, const char *name
,
118 int64_t ts
, bool *lazy_update
)
124 *lazy_update
= false;
130 timeval
= ts
/ NSEC_PER_SEC
;
132 if (bound
->lazy_values
.gmt
) {
133 /* Get day, month, year. */
134 if (!bt_gmtime_r(&timeval
, &tm
)) {
135 BT_LOGE_STR("Failure in bt_gmtime_r().");
138 tm
.tm_sec
= bound
->lazy_values
.ss
;
139 tm
.tm_min
= bound
->lazy_values
.mm
;
140 tm
.tm_hour
= bound
->lazy_values
.hh
;
141 timeval
= bt_timegm(&tm
);
143 BT_LOGE("Failure in bt_timegm(), incorrectly formatted %s timestamp",
148 /* Get day, month, year. */
149 if (!bt_localtime_r(&timeval
, &tm
)) {
150 BT_LOGE_STR("Failure in bt_localtime_r().");
153 tm
.tm_sec
= bound
->lazy_values
.ss
;
154 tm
.tm_min
= bound
->lazy_values
.mm
;
155 tm
.tm_hour
= bound
->lazy_values
.hh
;
156 timeval
= mktime(&tm
);
158 BT_LOGE("Failure in mktime(), incorrectly formatted %s timestamp",
163 value
= (int64_t) timeval
;
164 value
*= NSEC_PER_SEC
;
165 value
+= bound
->lazy_values
.ns
;
166 bound
->value
= value
;
177 const struct bt_notification
*evaluate_event_notification(
178 const struct bt_notification
*notification
,
179 struct trimmer_iterator
*trim_it
,
180 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
181 bool *_event_in_range
, bool *finished
)
185 const struct bt_event
*event
= NULL
, *writer_event
;
186 bool in_range
= true;
187 const struct bt_clock_class
*clock_class
= NULL
;
188 const struct bt_trace
*trace
= NULL
;
189 const struct bt_stream
*stream
= NULL
;
190 const struct bt_stream_class
*stream_class
= NULL
;
191 struct bt_clock_value
*clock_value
= NULL
;
192 bool lazy_update
= false;
193 const struct bt_notification
*new_notification
= NULL
;
194 struct bt_clock_class_priority_map
*cc_prio_map
;
196 event
= bt_notification_event_get_event(notification
);
198 cc_prio_map
= bt_notification_event_get_clock_class_priority_map(
200 BT_ASSERT(cc_prio_map
);
201 writer_event
= trimmer_output_event(trim_it
, event
);
202 BT_ASSERT(writer_event
);
203 new_notification
= bt_notification_event_create(writer_event
, cc_prio_map
);
204 BT_ASSERT(new_notification
);
205 bt_object_put_ref(cc_prio_map
);
207 stream
= bt_event_get_stream(event
);
210 stream_class
= bt_stream_get_class(stream
);
211 BT_ASSERT(stream_class
);
213 trace
= bt_stream_class_get_trace(stream_class
);
216 /* FIXME multi-clock? */
217 clock_class
= bt_trace_get_clock_class_by_index(trace
, 0);
222 clock_value
= bt_event_get_clock_value(event
, clock_class
);
224 BT_LOGE_STR("Failed to retrieve clock value.");
228 clock_ret
= bt_clock_value_get_value_ns_from_epoch(
231 BT_LOGE_STR("Failed to retrieve clock value timestamp.");
234 if (update_lazy_bound(begin
, "begin", ts
, &lazy_update
)) {
237 if (update_lazy_bound(end
, "end", ts
, &lazy_update
)) {
240 if (lazy_update
&& begin
->set
&& end
->set
) {
241 if (begin
->value
> end
->value
) {
242 BT_LOGE_STR("Unexpected: time range begin value is above end value.");
246 if (begin
->set
&& ts
< begin
->value
) {
249 if (end
->set
&& ts
> end
->value
) {
257 BT_OBJECT_PUT_REF_AND_RESET(new_notification
);
259 bt_object_put_ref(event
);
260 bt_object_put_ref(writer_event
);
261 bt_object_put_ref(clock_class
);
262 bt_object_put_ref(trace
);
263 bt_object_put_ref(stream
);
264 bt_object_put_ref(stream_class
);
265 bt_object_put_ref(clock_value
);
266 *_event_in_range
= in_range
;
267 return new_notification
;
271 int ns_from_integer_field(const struct bt_field
*integer
, int64_t *ns
)
275 uint64_t raw_clock_value
;
276 const struct bt_field_class
*integer_class
= NULL
;
277 const struct bt_clock_class
*clock_class
= NULL
;
278 struct bt_clock_value
*clock_value
= NULL
;
280 integer_class
= bt_field_get_class(integer
);
281 BT_ASSERT(integer_class
);
282 clock_class
= bt_field_class_integer_get_mapped_clock_class(
289 is_signed
= bt_field_class_integer_is_signed(integer_class
);
291 ret
= bt_field_unsigned_integer_get_value(integer
,
297 /* Signed clock values are unsupported. */
302 clock_value
= bt_clock_value_create(clock_class
, raw_clock_value
);
307 ret
= bt_clock_value_get_value_ns_from_epoch(clock_value
, ns
);
309 bt_object_put_ref(integer_class
);
310 bt_object_put_ref(clock_class
);
311 bt_object_put_ref(clock_value
);
315 static uint64_t ns_from_value(uint64_t frequency
, uint64_t value
)
319 if (frequency
== NSEC_PER_SEC
) {
322 ns
= (uint64_t) ((1e9
* (double) value
) / (double) frequency
);
329 * timestamp minus the offset.
332 int64_t get_raw_timestamp(const struct bt_packet
*writer_packet
,
335 const struct bt_clock_class
*writer_clock_class
;
336 int64_t sec_offset
, cycles_offset
, ns
;
337 const struct bt_trace
*writer_trace
;
338 const struct bt_stream
*writer_stream
;
339 const struct bt_stream_class
*writer_stream_class
;
343 writer_stream
= bt_packet_get_stream(writer_packet
);
344 BT_ASSERT(writer_stream
);
346 writer_stream_class
= bt_stream_get_class(writer_stream
);
347 BT_ASSERT(writer_stream_class
);
349 writer_trace
= bt_stream_class_get_trace(writer_stream_class
);
350 BT_ASSERT(writer_trace
);
352 /* FIXME multi-clock? */
353 writer_clock_class
= bt_trace_get_clock_class_by_index(
355 BT_ASSERT(writer_clock_class
);
357 ret
= bt_clock_class_get_offset_s(writer_clock_class
, &sec_offset
);
359 ns
= sec_offset
* NSEC_PER_SEC
;
361 freq
= bt_clock_class_get_frequency(writer_clock_class
);
362 BT_ASSERT(freq
!= -1ULL);
364 ret
= bt_clock_class_get_offset_cycles(writer_clock_class
, &cycles_offset
);
367 ns
+= ns_from_value(freq
, cycles_offset
);
369 bt_object_put_ref(writer_clock_class
);
370 bt_object_put_ref(writer_trace
);
371 bt_object_put_ref(writer_stream_class
);
372 bt_object_put_ref(writer_stream
);
374 return timestamp
- ns
;
378 const struct bt_notification
*evaluate_packet_notification(
379 const struct bt_notification
*notification
,
380 struct trimmer_iterator
*trim_it
,
381 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
382 bool *_packet_in_range
, bool *finished
)
384 int64_t begin_ns
, pkt_begin_ns
, end_ns
, pkt_end_ns
;
385 bool in_range
= true;
386 const struct bt_packet
*packet
= NULL
, *writer_packet
= NULL
;
387 const struct bt_field
*packet_context
= NULL
,
388 *timestamp_begin
= NULL
,
389 *timestamp_end
= NULL
;
390 const struct bt_notification
*new_notification
= NULL
;
391 enum bt_component_status ret
;
392 bool lazy_update
= false;
394 switch (bt_notification_get_type(notification
)) {
395 case BT_NOTIFICATION_TYPE_PACKET_BEGINNING
:
396 packet
= bt_notification_packet_beginning_get_packet(notification
);
398 writer_packet
= trimmer_new_packet(trim_it
, packet
);
399 BT_ASSERT(writer_packet
);
401 case BT_NOTIFICATION_TYPE_PACKET_END
:
402 packet
= bt_notification_packet_end_get_packet(notification
);
404 writer_packet
= trimmer_close_packet(trim_it
, packet
);
405 BT_ASSERT(writer_packet
);
411 packet_context
= bt_packet_get_context(writer_packet
);
412 if (!packet_context
) {
416 if (!bt_field_is_structure(packet_context
)) {
420 timestamp_begin
= bt_field_structure_get_field_by_name(
421 packet_context
, "timestamp_begin");
422 if (!timestamp_begin
|| !bt_field_is_integer(timestamp_begin
)) {
425 timestamp_end
= bt_field_structure_get_field_by_name(
426 packet_context
, "timestamp_end");
427 if (!timestamp_end
|| !bt_field_is_integer(timestamp_end
)) {
431 if (ns_from_integer_field(timestamp_begin
, &pkt_begin_ns
)) {
434 if (ns_from_integer_field(timestamp_end
, &pkt_end_ns
)) {
438 if (update_lazy_bound(begin
, "begin", pkt_begin_ns
, &lazy_update
)) {
441 if (update_lazy_bound(end
, "end", pkt_end_ns
, &lazy_update
)) {
444 if (lazy_update
&& begin
->set
&& end
->set
) {
445 if (begin
->value
> end
->value
) {
446 BT_LOGE_STR("Unexpected: time range begin value is above end value.");
451 begin_ns
= begin
->set
? begin
->value
: INT64_MIN
;
452 end_ns
= end
->set
? end
->value
: INT64_MAX
;
455 * Accept if there is any overlap between the selected region and the
458 in_range
= (pkt_end_ns
>= begin_ns
) && (pkt_begin_ns
<= end_ns
);
462 if (pkt_begin_ns
> end_ns
) {
466 if (begin_ns
> pkt_begin_ns
) {
467 ret
= update_packet_context_field(trim_it
->err
, writer_packet
,
469 get_raw_timestamp(writer_packet
, begin_ns
));
473 if (end_ns
< pkt_end_ns
) {
474 ret
= update_packet_context_field(trim_it
->err
, writer_packet
,
476 get_raw_timestamp(writer_packet
, end_ns
));
481 switch (bt_notification_get_type(notification
)) {
482 case BT_NOTIFICATION_TYPE_PACKET_BEGINNING
:
483 new_notification
= bt_notification_packet_beginning_create(writer_packet
);
484 BT_ASSERT(new_notification
);
486 case BT_NOTIFICATION_TYPE_PACKET_END
:
487 new_notification
= bt_notification_packet_end_create(writer_packet
);
488 BT_ASSERT(new_notification
);
494 *_packet_in_range
= in_range
;
495 bt_object_put_ref(packet
);
496 bt_object_put_ref(writer_packet
);
497 bt_object_put_ref(packet_context
);
498 bt_object_put_ref(timestamp_begin
);
499 bt_object_put_ref(timestamp_end
);
500 return new_notification
;
504 const struct bt_notification
*evaluate_stream_notification(
505 const struct bt_notification
*notification
,
506 struct trimmer_iterator
*trim_it
)
508 const struct bt_stream
*stream
;
510 stream
= bt_notification_stream_end_get_stream(notification
);
513 /* FIXME: useless copy */
514 return bt_notification_stream_end_create(stream
);
517 /* Return true if the notification should be forwarded. */
519 enum bt_notification_iterator_status
evaluate_notification(
520 const struct bt_notification
**notification
,
521 struct trimmer_iterator
*trim_it
,
522 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
525 enum bt_notification_type type
;
526 const struct bt_notification
*new_notification
= NULL
;
527 bool finished
= false;
530 type
= bt_notification_get_type(*notification
);
532 case BT_NOTIFICATION_TYPE_EVENT
:
533 new_notification
= evaluate_event_notification(*notification
,
534 trim_it
, begin
, end
, in_range
, &finished
);
536 case BT_NOTIFICATION_TYPE_PACKET_BEGINNING
:
537 case BT_NOTIFICATION_TYPE_PACKET_END
:
538 new_notification
= evaluate_packet_notification(*notification
,
539 trim_it
, begin
, end
, in_range
, &finished
);
541 case BT_NOTIFICATION_TYPE_STREAM_END
:
542 new_notification
= evaluate_stream_notification(*notification
,
548 BT_OBJECT_PUT_REF_AND_RESET(*notification
);
549 *notification
= new_notification
;
552 return BT_NOTIFICATION_ITERATOR_STATUS_END
;
555 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
559 struct bt_notification_iterator_next_method_return
trimmer_iterator_next(
560 struct bt_self_notification_iterator
*iterator
)
562 struct trimmer_iterator
*trim_it
= NULL
;
563 struct bt_self_component
*component
= NULL
;
564 struct trimmer
*trimmer
= NULL
;
565 struct bt_notification_iterator
*source_it
= NULL
;
566 struct bt_notification_iterator_next_method_return ret
= {
567 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
568 .notification
= NULL
,
570 bool notification_in_range
= false;
572 trim_it
= bt_self_notification_iterator_get_user_data(iterator
);
575 component
= bt_self_notification_iterator_get_private_component(
577 BT_ASSERT(component
);
578 trimmer
= bt_self_component_get_user_data(component
);
581 source_it
= trim_it
->input_iterator
;
582 BT_ASSERT(source_it
);
584 while (!notification_in_range
) {
585 ret
.status
= bt_notification_iterator_next(source_it
);
586 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
590 ret
.notification
= bt_notification_iterator_get_notification(
592 if (!ret
.notification
) {
593 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
597 ret
.status
= evaluate_notification(&ret
.notification
, trim_it
,
598 &trimmer
->begin
, &trimmer
->end
,
599 ¬ification_in_range
);
600 if (!notification_in_range
) {
601 BT_OBJECT_PUT_REF_AND_RESET(ret
.notification
);
604 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
609 bt_object_put_ref(component
);