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 struct bt_packet
*writer_packet
= value
;
47 bt_put(writer_packet
);
52 void trimmer_iterator_finalize(struct bt_private_connection_private_notification_iterator
*it
)
54 struct trimmer_iterator
*trim_it
;
56 trim_it
= bt_private_connection_private_notification_iterator_get_user_data(it
);
59 bt_put(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_private_connection_private_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_private_component
*component
=
78 bt_private_connection_private_notification_iterator_get_private_component(iterator
);
79 struct trimmer_iterator
*it_data
= g_new0(struct trimmer_iterator
, 1);
80 static const enum bt_notification_type notif_types
[] = {
81 BT_NOTIFICATION_TYPE_EVENT
,
82 BT_NOTIFICATION_TYPE_STREAM_END
,
83 BT_NOTIFICATION_TYPE_PACKET_BEGIN
,
84 BT_NOTIFICATION_TYPE_PACKET_END
,
85 BT_NOTIFICATION_TYPE_SENTINEL
,
89 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
93 /* Create a new iterator on the upstream component. */
94 input_port
= bt_private_component_filter_get_input_private_port_by_name(
96 BT_ASSERT(input_port
);
97 connection
= bt_private_port_get_private_connection(input_port
);
98 BT_ASSERT(connection
);
100 conn_status
= bt_private_connection_create_notification_iterator(connection
,
101 notif_types
, &it_data
->input_iterator
);
102 if (conn_status
!= BT_CONNECTION_STATUS_OK
) {
103 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
107 it_data
->err
= stderr
;
108 it_data
->packet_map
= g_hash_table_new_full(g_direct_hash
,
109 g_direct_equal
, NULL
, NULL
);
111 it_ret
= bt_private_connection_private_notification_iterator_set_user_data(iterator
,
124 int update_lazy_bound(struct trimmer_bound
*bound
, const char *name
,
125 int64_t ts
, bool *lazy_update
)
131 *lazy_update
= false;
137 timeval
= ts
/ NSEC_PER_SEC
;
139 if (bound
->lazy_values
.gmt
) {
140 /* Get day, month, year. */
141 if (!bt_gmtime_r(&timeval
, &tm
)) {
142 BT_LOGE_STR("Failure in bt_gmtime_r().");
145 tm
.tm_sec
= bound
->lazy_values
.ss
;
146 tm
.tm_min
= bound
->lazy_values
.mm
;
147 tm
.tm_hour
= bound
->lazy_values
.hh
;
148 timeval
= bt_timegm(&tm
);
150 BT_LOGE("Failure in bt_timegm(), incorrectly formatted %s timestamp",
155 /* Get day, month, year. */
156 if (!bt_localtime_r(&timeval
, &tm
)) {
157 BT_LOGE_STR("Failure in bt_localtime_r().");
160 tm
.tm_sec
= bound
->lazy_values
.ss
;
161 tm
.tm_min
= bound
->lazy_values
.mm
;
162 tm
.tm_hour
= bound
->lazy_values
.hh
;
163 timeval
= mktime(&tm
);
165 BT_LOGE("Failure in mktime(), incorrectly formatted %s timestamp",
170 value
= (int64_t) timeval
;
171 value
*= NSEC_PER_SEC
;
172 value
+= bound
->lazy_values
.ns
;
173 bound
->value
= value
;
184 struct bt_notification
*evaluate_event_notification(
185 struct bt_notification
*notification
,
186 struct trimmer_iterator
*trim_it
,
187 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
188 bool *_event_in_range
, bool *finished
)
192 struct bt_event
*event
= NULL
, *writer_event
;
193 bool in_range
= true;
194 struct bt_clock_class
*clock_class
= NULL
;
195 struct bt_trace
*trace
= NULL
;
196 struct bt_stream
*stream
= NULL
;
197 struct bt_stream_class
*stream_class
= NULL
;
198 struct bt_clock_value
*clock_value
= NULL
;
199 bool lazy_update
= false;
200 struct bt_notification
*new_notification
= NULL
;
201 struct bt_clock_class_priority_map
*cc_prio_map
;
203 event
= bt_notification_event_get_event(notification
);
205 cc_prio_map
= bt_notification_event_get_clock_class_priority_map(
207 BT_ASSERT(cc_prio_map
);
208 writer_event
= trimmer_output_event(trim_it
, event
);
209 BT_ASSERT(writer_event
);
210 new_notification
= bt_notification_event_create(writer_event
, cc_prio_map
);
211 BT_ASSERT(new_notification
);
214 stream
= bt_event_get_stream(event
);
217 stream_class
= bt_stream_get_class(stream
);
218 BT_ASSERT(stream_class
);
220 trace
= bt_stream_class_get_trace(stream_class
);
223 /* FIXME multi-clock? */
224 clock_class
= bt_trace_get_clock_class_by_index(trace
, 0);
229 clock_value
= bt_event_get_clock_value(event
, clock_class
);
231 BT_LOGE_STR("Failed to retrieve clock value.");
235 clock_ret
= bt_clock_value_get_value_ns_from_epoch(
238 BT_LOGE_STR("Failed to retrieve clock value timestamp.");
241 if (update_lazy_bound(begin
, "begin", ts
, &lazy_update
)) {
244 if (update_lazy_bound(end
, "end", ts
, &lazy_update
)) {
247 if (lazy_update
&& begin
->set
&& end
->set
) {
248 if (begin
->value
> end
->value
) {
249 BT_LOGE_STR("Unexpected: time range begin value is above end value.");
253 if (begin
->set
&& ts
< begin
->value
) {
256 if (end
->set
&& ts
> end
->value
) {
264 BT_PUT(new_notification
);
267 bt_put(writer_event
);
271 bt_put(stream_class
);
273 *_event_in_range
= in_range
;
274 return new_notification
;
278 int ns_from_integer_field(struct bt_field
*integer
, int64_t *ns
)
282 uint64_t raw_clock_value
;
283 struct bt_field_type
*integer_type
= NULL
;
284 struct bt_clock_class
*clock_class
= NULL
;
285 struct bt_clock_value
*clock_value
= NULL
;
287 integer_type
= bt_field_get_type(integer
);
288 BT_ASSERT(integer_type
);
289 clock_class
= bt_field_type_integer_get_mapped_clock_class(
296 is_signed
= bt_ctf_field_type_integer_get_signed(integer_type
);
298 ret
= bt_field_unsigned_integer_get_value(integer
,
304 /* Signed clock values are unsupported. */
309 clock_value
= bt_clock_value_create(clock_class
, raw_clock_value
);
314 ret
= bt_clock_value_get_value_ns_from_epoch(clock_value
, ns
);
316 bt_put(integer_type
);
322 static uint64_t ns_from_value(uint64_t frequency
, uint64_t value
)
326 if (frequency
== NSEC_PER_SEC
) {
329 ns
= (uint64_t) ((1e9
* (double) value
) / (double) frequency
);
336 * timestamp minus the offset.
339 int64_t get_raw_timestamp(struct bt_packet
*writer_packet
,
342 struct bt_clock_class
*writer_clock_class
;
343 int64_t sec_offset
, cycles_offset
, ns
;
344 struct bt_trace
*writer_trace
;
345 struct bt_stream
*writer_stream
;
346 struct bt_stream_class
*writer_stream_class
;
350 writer_stream
= bt_packet_get_stream(writer_packet
);
351 BT_ASSERT(writer_stream
);
353 writer_stream_class
= bt_stream_get_class(writer_stream
);
354 BT_ASSERT(writer_stream_class
);
356 writer_trace
= bt_stream_class_get_trace(writer_stream_class
);
357 BT_ASSERT(writer_trace
);
359 /* FIXME multi-clock? */
360 writer_clock_class
= bt_trace_get_clock_class_by_index(
362 BT_ASSERT(writer_clock_class
);
364 ret
= bt_clock_class_get_offset_s(writer_clock_class
, &sec_offset
);
366 ns
= sec_offset
* NSEC_PER_SEC
;
368 freq
= bt_clock_class_get_frequency(writer_clock_class
);
369 BT_ASSERT(freq
!= -1ULL);
371 ret
= bt_clock_class_get_offset_cycles(writer_clock_class
, &cycles_offset
);
374 ns
+= ns_from_value(freq
, cycles_offset
);
376 bt_put(writer_clock_class
);
377 bt_put(writer_trace
);
378 bt_put(writer_stream_class
);
379 bt_put(writer_stream
);
381 return timestamp
- ns
;
385 struct bt_notification
*evaluate_packet_notification(
386 struct bt_notification
*notification
,
387 struct trimmer_iterator
*trim_it
,
388 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
389 bool *_packet_in_range
, bool *finished
)
391 int64_t begin_ns
, pkt_begin_ns
, end_ns
, pkt_end_ns
;
392 bool in_range
= true;
393 struct bt_packet
*packet
= NULL
, *writer_packet
= NULL
;
394 struct bt_field
*packet_context
= NULL
,
395 *timestamp_begin
= NULL
,
396 *timestamp_end
= NULL
;
397 struct bt_notification
*new_notification
= NULL
;
398 enum bt_component_status ret
;
399 bool lazy_update
= false;
401 switch (bt_notification_get_type(notification
)) {
402 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
403 packet
= bt_notification_packet_begin_get_packet(notification
);
405 writer_packet
= trimmer_new_packet(trim_it
, packet
);
406 BT_ASSERT(writer_packet
);
408 case BT_NOTIFICATION_TYPE_PACKET_END
:
409 packet
= bt_notification_packet_end_get_packet(notification
);
411 writer_packet
= trimmer_close_packet(trim_it
, packet
);
412 BT_ASSERT(writer_packet
);
418 packet_context
= bt_packet_get_context(writer_packet
);
419 if (!packet_context
) {
423 if (!bt_field_is_structure(packet_context
)) {
427 timestamp_begin
= bt_field_structure_get_field_by_name(
428 packet_context
, "timestamp_begin");
429 if (!timestamp_begin
|| !bt_field_is_integer(timestamp_begin
)) {
432 timestamp_end
= bt_field_structure_get_field_by_name(
433 packet_context
, "timestamp_end");
434 if (!timestamp_end
|| !bt_field_is_integer(timestamp_end
)) {
438 if (ns_from_integer_field(timestamp_begin
, &pkt_begin_ns
)) {
441 if (ns_from_integer_field(timestamp_end
, &pkt_end_ns
)) {
445 if (update_lazy_bound(begin
, "begin", pkt_begin_ns
, &lazy_update
)) {
448 if (update_lazy_bound(end
, "end", pkt_end_ns
, &lazy_update
)) {
451 if (lazy_update
&& begin
->set
&& end
->set
) {
452 if (begin
->value
> end
->value
) {
453 BT_LOGE_STR("Unexpected: time range begin value is above end value.");
458 begin_ns
= begin
->set
? begin
->value
: INT64_MIN
;
459 end_ns
= end
->set
? end
->value
: INT64_MAX
;
462 * Accept if there is any overlap between the selected region and the
465 in_range
= (pkt_end_ns
>= begin_ns
) && (pkt_begin_ns
<= end_ns
);
469 if (pkt_begin_ns
> end_ns
) {
473 if (begin_ns
> pkt_begin_ns
) {
474 ret
= update_packet_context_field(trim_it
->err
, writer_packet
,
476 get_raw_timestamp(writer_packet
, begin_ns
));
480 if (end_ns
< pkt_end_ns
) {
481 ret
= update_packet_context_field(trim_it
->err
, writer_packet
,
483 get_raw_timestamp(writer_packet
, end_ns
));
488 switch (bt_notification_get_type(notification
)) {
489 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
490 new_notification
= bt_notification_packet_begin_create(writer_packet
);
491 BT_ASSERT(new_notification
);
493 case BT_NOTIFICATION_TYPE_PACKET_END
:
494 new_notification
= bt_notification_packet_end_create(writer_packet
);
495 BT_ASSERT(new_notification
);
501 *_packet_in_range
= in_range
;
503 bt_put(writer_packet
);
504 bt_put(packet_context
);
505 bt_put(timestamp_begin
);
506 bt_put(timestamp_end
);
507 return new_notification
;
511 struct bt_notification
*evaluate_stream_notification(
512 struct bt_notification
*notification
,
513 struct trimmer_iterator
*trim_it
)
515 struct bt_stream
*stream
;
517 stream
= bt_notification_stream_end_get_stream(notification
);
520 /* FIXME: useless copy */
521 return bt_notification_stream_end_create(stream
);
524 /* Return true if the notification should be forwarded. */
526 enum bt_notification_iterator_status
evaluate_notification(
527 struct bt_notification
**notification
,
528 struct trimmer_iterator
*trim_it
,
529 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
532 enum bt_notification_type type
;
533 struct bt_notification
*new_notification
= NULL
;
534 bool finished
= false;
537 type
= bt_notification_get_type(*notification
);
539 case BT_NOTIFICATION_TYPE_EVENT
:
540 new_notification
= evaluate_event_notification(*notification
,
541 trim_it
, begin
, end
, in_range
, &finished
);
543 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
544 case BT_NOTIFICATION_TYPE_PACKET_END
:
545 new_notification
= evaluate_packet_notification(*notification
,
546 trim_it
, begin
, end
, in_range
, &finished
);
548 case BT_NOTIFICATION_TYPE_STREAM_END
:
549 new_notification
= evaluate_stream_notification(*notification
,
553 puts("Unhandled notification type");
556 BT_PUT(*notification
);
557 *notification
= new_notification
;
560 return BT_NOTIFICATION_ITERATOR_STATUS_END
;
563 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
567 struct bt_notification_iterator_next_method_return
trimmer_iterator_next(
568 struct bt_private_connection_private_notification_iterator
*iterator
)
570 struct trimmer_iterator
*trim_it
= NULL
;
571 struct bt_private_component
*component
= NULL
;
572 struct trimmer
*trimmer
= NULL
;
573 struct bt_notification_iterator
*source_it
= NULL
;
574 struct bt_notification_iterator_next_method_return ret
= {
575 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
576 .notification
= NULL
,
578 bool notification_in_range
= false;
580 trim_it
= bt_private_connection_private_notification_iterator_get_user_data(iterator
);
583 component
= bt_private_connection_private_notification_iterator_get_private_component(
585 BT_ASSERT(component
);
586 trimmer
= bt_private_component_get_user_data(component
);
589 source_it
= trim_it
->input_iterator
;
590 BT_ASSERT(source_it
);
592 while (!notification_in_range
) {
593 ret
.status
= bt_notification_iterator_next(source_it
);
594 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
598 ret
.notification
= bt_notification_iterator_get_notification(
600 if (!ret
.notification
) {
601 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
605 ret
.status
= evaluate_notification(&ret
.notification
, trim_it
,
606 &trimmer
->begin
, &trimmer
->end
,
607 ¬ification_in_range
);
608 if (!notification_in_range
) {
609 BT_PUT(ret
.notification
);
612 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {