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 #include <babeltrace/compat/time-internal.h>
30 #include <babeltrace/compat/utc-internal.h>
31 #include <babeltrace/graph/notification-iterator.h>
32 #include <babeltrace/graph/private-notification-iterator.h>
33 #include <babeltrace/graph/notification.h>
34 #include <babeltrace/graph/notification-event.h>
35 #include <babeltrace/graph/notification-stream.h>
36 #include <babeltrace/graph/notification-packet.h>
37 #include <babeltrace/graph/component-filter.h>
38 #include <babeltrace/graph/private-component-filter.h>
39 #include <babeltrace/graph/private-port.h>
40 #include <babeltrace/graph/private-connection.h>
41 #include <babeltrace/graph/private-component.h>
42 #include <babeltrace/ctf-ir/event.h>
43 #include <babeltrace/ctf-ir/stream.h>
44 #include <babeltrace/ctf-ir/stream-class.h>
45 #include <babeltrace/ctf-ir/clock-class.h>
46 #include <babeltrace/ctf-ir/packet.h>
47 #include <babeltrace/ctf-ir/trace.h>
48 #include <babeltrace/ctf-ir/fields.h>
50 #include <plugins-common.h>
57 gboolean
close_packets(gpointer key
, gpointer value
, gpointer user_data
)
59 struct bt_ctf_packet
*writer_packet
= value
;
61 bt_put(writer_packet
);
66 void trimmer_iterator_finalize(struct bt_private_notification_iterator
*it
)
68 struct trimmer_iterator
*trim_it
;
70 trim_it
= bt_private_notification_iterator_get_user_data(it
);
73 bt_put(trim_it
->input_iterator
);
74 g_hash_table_foreach_remove(trim_it
->packet_map
,
76 g_hash_table_destroy(trim_it
->packet_map
);
81 enum bt_notification_iterator_status
trimmer_iterator_init(
82 struct bt_private_notification_iterator
*iterator
,
83 struct bt_private_port
*port
)
85 enum bt_notification_iterator_status ret
=
86 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
87 enum bt_notification_iterator_status it_ret
;
88 struct bt_private_port
*input_port
= NULL
;
89 struct bt_private_connection
*connection
= NULL
;
90 struct bt_private_component
*component
=
91 bt_private_notification_iterator_get_private_component(iterator
);
92 struct trimmer_iterator
*it_data
= g_new0(struct trimmer_iterator
, 1);
93 static const enum bt_notification_type notif_types
[] = {
94 BT_NOTIFICATION_TYPE_EVENT
,
95 BT_NOTIFICATION_TYPE_STREAM_END
,
96 BT_NOTIFICATION_TYPE_PACKET_BEGIN
,
97 BT_NOTIFICATION_TYPE_PACKET_END
,
98 BT_NOTIFICATION_TYPE_SENTINEL
,
102 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
106 /* Create a new iterator on the upstream component. */
107 input_port
= bt_private_component_filter_get_input_private_port_by_name(
110 connection
= bt_private_port_get_private_connection(input_port
);
113 it_data
->input_iterator
=
114 bt_private_connection_create_notification_iterator(connection
,
116 if (!it_data
->input_iterator
) {
117 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
121 it_data
->err
= stderr
;
122 it_data
->packet_map
= g_hash_table_new_full(g_direct_hash
,
123 g_direct_equal
, NULL
, NULL
);
125 it_ret
= bt_private_notification_iterator_set_user_data(iterator
,
138 int update_lazy_bound(struct trimmer_bound
*bound
, const char *name
,
139 int64_t ts
, bool *lazy_update
)
145 *lazy_update
= false;
151 timeval
= ts
/ NSEC_PER_SEC
;
153 if (bound
->lazy_values
.gmt
) {
154 /* Get day, month, year. */
155 if (!bt_gmtime_r(&timeval
, &tm
)) {
156 printf_error("Failure in bt_gmtime_r()");
159 tm
.tm_sec
= bound
->lazy_values
.ss
;
160 tm
.tm_min
= bound
->lazy_values
.mm
;
161 tm
.tm_hour
= bound
->lazy_values
.hh
;
162 timeval
= bt_timegm(&tm
);
164 printf_error("Failure in bt_timegm(), incorrectly formatted %s timestamp",
169 /* Get day, month, year. */
170 if (!bt_localtime_r(&timeval
, &tm
)) {
171 printf_error("Failure in bt_localtime_r()");
174 tm
.tm_sec
= bound
->lazy_values
.ss
;
175 tm
.tm_min
= bound
->lazy_values
.mm
;
176 tm
.tm_hour
= bound
->lazy_values
.hh
;
177 timeval
= mktime(&tm
);
179 printf_error("Failure in mktime(), incorrectly formatted %s timestamp",
184 value
= (int64_t) timeval
;
185 value
*= NSEC_PER_SEC
;
186 value
+= bound
->lazy_values
.ns
;
187 bound
->value
= value
;
198 struct bt_notification
*evaluate_event_notification(
199 struct bt_notification
*notification
,
200 struct trimmer_iterator
*trim_it
,
201 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
202 bool *_event_in_range
, bool *finished
)
206 struct bt_ctf_event
*event
= NULL
, *writer_event
;
207 bool in_range
= true;
208 struct bt_ctf_clock_class
*clock_class
= NULL
;
209 struct bt_ctf_trace
*trace
= NULL
;
210 struct bt_ctf_stream
*stream
= NULL
;
211 struct bt_ctf_stream_class
*stream_class
= NULL
;
212 struct bt_ctf_clock_value
*clock_value
= NULL
;
213 bool lazy_update
= false;
214 struct bt_notification
*new_notification
= NULL
;
215 struct bt_clock_class_priority_map
*cc_prio_map
;
217 event
= bt_notification_event_get_event(notification
);
219 cc_prio_map
= bt_notification_event_get_clock_class_priority_map(
222 writer_event
= trimmer_output_event(trim_it
, event
);
223 assert(writer_event
);
224 new_notification
= bt_notification_event_create(writer_event
, cc_prio_map
);
225 assert(new_notification
);
228 stream
= bt_ctf_event_get_stream(event
);
231 stream_class
= bt_ctf_stream_get_class(stream
);
232 assert(stream_class
);
234 trace
= bt_ctf_stream_class_get_trace(stream_class
);
237 /* FIXME multi-clock? */
238 clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, 0);
243 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
245 printf_error("Failed to retrieve clock value");
249 clock_ret
= bt_ctf_clock_value_get_value_ns_from_epoch(
252 printf_error("Failed to retrieve clock value timestamp");
255 if (update_lazy_bound(begin
, "begin", ts
, &lazy_update
)) {
258 if (update_lazy_bound(end
, "end", ts
, &lazy_update
)) {
261 if (lazy_update
&& begin
->set
&& end
->set
) {
262 if (begin
->value
> end
->value
) {
263 printf_error("Unexpected: time range begin value is above end value");
267 if (begin
->set
&& ts
< begin
->value
) {
270 if (end
->set
&& ts
> end
->value
) {
278 BT_PUT(new_notification
);
281 bt_put(writer_event
);
285 bt_put(stream_class
);
287 *_event_in_range
= in_range
;
288 return new_notification
;
292 int ns_from_integer_field(struct bt_ctf_field
*integer
, int64_t *ns
)
296 uint64_t raw_clock_value
;
297 struct bt_ctf_field_type
*integer_type
= NULL
;
298 struct bt_ctf_clock_class
*clock_class
= NULL
;
299 struct bt_ctf_clock_value
*clock_value
= NULL
;
301 integer_type
= bt_ctf_field_get_type(integer
);
302 assert(integer_type
);
303 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
310 is_signed
= bt_ctf_field_type_integer_get_signed(integer_type
);
312 ret
= bt_ctf_field_unsigned_integer_get_value(integer
,
318 /* Signed clock values are unsupported. */
323 clock_value
= bt_ctf_clock_value_create(clock_class
, raw_clock_value
);
328 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
, ns
);
330 bt_put(integer_type
);
336 static uint64_t ns_from_value(uint64_t frequency
, uint64_t value
)
340 if (frequency
== NSEC_PER_SEC
) {
343 ns
= (uint64_t) ((1e9
* (double) value
) / (double) frequency
);
350 * timestamp minus the offset.
353 int64_t get_raw_timestamp(struct bt_ctf_packet
*writer_packet
,
356 struct bt_ctf_clock_class
*writer_clock_class
;
357 int64_t sec_offset
, cycles_offset
, ns
;
358 struct bt_ctf_trace
*writer_trace
;
359 struct bt_ctf_stream
*writer_stream
;
360 struct bt_ctf_stream_class
*writer_stream_class
;
364 writer_stream
= bt_ctf_packet_get_stream(writer_packet
);
365 assert(writer_stream
);
367 writer_stream_class
= bt_ctf_stream_get_class(writer_stream
);
368 assert(writer_stream_class
);
370 writer_trace
= bt_ctf_stream_class_get_trace(writer_stream_class
);
371 assert(writer_trace
);
373 /* FIXME multi-clock? */
374 writer_clock_class
= bt_ctf_trace_get_clock_class_by_index(
376 assert(writer_clock_class
);
378 ret
= bt_ctf_clock_class_get_offset_s(writer_clock_class
, &sec_offset
);
380 ns
= sec_offset
* NSEC_PER_SEC
;
382 freq
= bt_ctf_clock_class_get_frequency(writer_clock_class
);
383 assert(freq
!= -1ULL);
385 ret
= bt_ctf_clock_class_get_offset_cycles(writer_clock_class
, &cycles_offset
);
388 ns
+= ns_from_value(freq
, cycles_offset
);
390 bt_put(writer_clock_class
);
391 bt_put(writer_trace
);
392 bt_put(writer_stream_class
);
393 bt_put(writer_stream
);
395 return timestamp
- ns
;
399 struct bt_notification
*evaluate_packet_notification(
400 struct bt_notification
*notification
,
401 struct trimmer_iterator
*trim_it
,
402 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
403 bool *_packet_in_range
, bool *finished
)
405 int64_t begin_ns
, pkt_begin_ns
, end_ns
, pkt_end_ns
;
406 bool in_range
= true;
407 struct bt_ctf_packet
*packet
= NULL
, *writer_packet
= NULL
;
408 struct bt_ctf_field
*packet_context
= NULL
,
409 *timestamp_begin
= NULL
,
410 *timestamp_end
= NULL
;
411 struct bt_notification
*new_notification
= NULL
;
412 enum bt_component_status ret
;
413 bool lazy_update
= false;
415 switch (bt_notification_get_type(notification
)) {
416 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
417 packet
= bt_notification_packet_begin_get_packet(notification
);
419 writer_packet
= trimmer_new_packet(trim_it
, packet
);
420 assert(writer_packet
);
422 case BT_NOTIFICATION_TYPE_PACKET_END
:
423 packet
= bt_notification_packet_end_get_packet(notification
);
425 writer_packet
= trimmer_close_packet(trim_it
, packet
);
426 assert(writer_packet
);
432 packet_context
= bt_ctf_packet_get_context(writer_packet
);
433 if (!packet_context
) {
437 if (!bt_ctf_field_is_structure(packet_context
)) {
441 timestamp_begin
= bt_ctf_field_structure_get_field(
442 packet_context
, "timestamp_begin");
443 if (!timestamp_begin
|| !bt_ctf_field_is_integer(timestamp_begin
)) {
446 timestamp_end
= bt_ctf_field_structure_get_field(
447 packet_context
, "timestamp_end");
448 if (!timestamp_end
|| !bt_ctf_field_is_integer(timestamp_end
)) {
452 if (ns_from_integer_field(timestamp_begin
, &pkt_begin_ns
)) {
455 if (ns_from_integer_field(timestamp_end
, &pkt_end_ns
)) {
459 if (update_lazy_bound(begin
, "begin", pkt_begin_ns
, &lazy_update
)) {
462 if (update_lazy_bound(end
, "end", pkt_end_ns
, &lazy_update
)) {
465 if (lazy_update
&& begin
->set
&& end
->set
) {
466 if (begin
->value
> end
->value
) {
467 printf_error("Unexpected: time range begin value is above end value");
472 begin_ns
= begin
->set
? begin
->value
: INT64_MIN
;
473 end_ns
= end
->set
? end
->value
: INT64_MAX
;
476 * Accept if there is any overlap between the selected region and the
479 in_range
= (pkt_end_ns
>= begin_ns
) && (pkt_begin_ns
<= end_ns
);
483 if (pkt_begin_ns
> end_ns
) {
487 if (begin_ns
> pkt_begin_ns
) {
488 ret
= update_packet_context_field(trim_it
->err
, writer_packet
,
490 get_raw_timestamp(writer_packet
, begin_ns
));
494 if (end_ns
< pkt_end_ns
) {
495 ret
= update_packet_context_field(trim_it
->err
, writer_packet
,
497 get_raw_timestamp(writer_packet
, end_ns
));
502 switch (bt_notification_get_type(notification
)) {
503 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
504 new_notification
= bt_notification_packet_begin_create(writer_packet
);
505 assert(new_notification
);
507 case BT_NOTIFICATION_TYPE_PACKET_END
:
508 new_notification
= bt_notification_packet_end_create(writer_packet
);
509 assert(new_notification
);
515 *_packet_in_range
= in_range
;
517 bt_put(writer_packet
);
518 bt_put(packet_context
);
519 bt_put(timestamp_begin
);
520 bt_put(timestamp_end
);
521 return new_notification
;
525 struct bt_notification
*evaluate_stream_notification(
526 struct bt_notification
*notification
,
527 struct trimmer_iterator
*trim_it
)
529 struct bt_ctf_stream
*stream
;
531 stream
= bt_notification_stream_end_get_stream(notification
);
534 /* FIXME: useless copy */
535 return bt_notification_stream_end_create(stream
);
538 /* Return true if the notification should be forwarded. */
540 enum bt_notification_iterator_status
evaluate_notification(
541 struct bt_notification
**notification
,
542 struct trimmer_iterator
*trim_it
,
543 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
546 enum bt_notification_type type
;
547 struct bt_notification
*new_notification
= NULL
;
548 bool finished
= false;
551 type
= bt_notification_get_type(*notification
);
553 case BT_NOTIFICATION_TYPE_EVENT
:
554 new_notification
= evaluate_event_notification(*notification
,
555 trim_it
, begin
, end
, in_range
, &finished
);
557 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
558 case BT_NOTIFICATION_TYPE_PACKET_END
:
559 new_notification
= evaluate_packet_notification(*notification
,
560 trim_it
, begin
, end
, in_range
, &finished
);
562 case BT_NOTIFICATION_TYPE_STREAM_END
:
563 new_notification
= evaluate_stream_notification(*notification
,
567 puts("Unhandled notification type");
570 BT_PUT(*notification
);
571 *notification
= new_notification
;
574 return BT_NOTIFICATION_ITERATOR_STATUS_END
;
577 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
581 struct bt_notification_iterator_next_return
trimmer_iterator_next(
582 struct bt_private_notification_iterator
*iterator
)
584 struct trimmer_iterator
*trim_it
= NULL
;
585 struct bt_private_component
*component
= NULL
;
586 struct trimmer
*trimmer
= NULL
;
587 struct bt_notification_iterator
*source_it
= NULL
;
588 struct bt_notification_iterator_next_return ret
= {
589 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
590 .notification
= NULL
,
592 bool notification_in_range
= false;
594 trim_it
= bt_private_notification_iterator_get_user_data(iterator
);
597 component
= bt_private_notification_iterator_get_private_component(
600 trimmer
= bt_private_component_get_user_data(component
);
603 source_it
= trim_it
->input_iterator
;
606 while (!notification_in_range
) {
607 ret
.status
= bt_notification_iterator_next(source_it
);
608 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
612 ret
.notification
= bt_notification_iterator_get_notification(
614 if (!ret
.notification
) {
615 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
619 ret
.status
= evaluate_notification(&ret
.notification
, trim_it
,
620 &trimmer
->begin
, &trimmer
->end
,
621 ¬ification_in_range
);
622 if (!notification_in_range
) {
623 BT_PUT(ret
.notification
);
626 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
636 enum bt_notification_iterator_status
trimmer_iterator_seek_time(
637 struct bt_private_notification_iterator
*iterator
,
640 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;