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/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>
40 #include <babeltrace/ctf-ir/event.h>
41 #include <babeltrace/ctf-ir/stream.h>
42 #include <babeltrace/ctf-ir/stream-class.h>
43 #include <babeltrace/ctf-ir/clock-class.h>
44 #include <babeltrace/ctf-ir/packet.h>
45 #include <babeltrace/ctf-ir/trace.h>
46 #include <babeltrace/ctf-ir/fields.h>
48 #include <plugins-common.h>
55 void trimmer_iterator_finalize(struct bt_private_notification_iterator
*it
)
57 struct trimmer_iterator
*it_data
;
59 it_data
= bt_private_notification_iterator_get_user_data(it
);
62 bt_put(it_data
->input_iterator
);
63 g_hash_table_destroy(it_data
->packet_map
);
68 enum bt_notification_iterator_status
trimmer_iterator_init(
69 struct bt_private_notification_iterator
*iterator
,
70 struct bt_private_port
*port
)
72 enum bt_notification_iterator_status ret
=
73 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
74 enum bt_notification_iterator_status it_ret
;
75 struct bt_private_port
*input_port
= NULL
;
76 struct bt_private_connection
*connection
= NULL
;
77 struct bt_private_component
*component
=
78 bt_private_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_private_component_filter_get_default_input_private_port(
90 connection
= bt_private_port_get_private_connection(input_port
);
93 it_data
->input_iterator
=
94 bt_private_connection_create_notification_iterator(connection
);
95 if (!it_data
->input_iterator
) {
96 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
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_private_notification_iterator_set_user_data(iterator
,
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 (!gmtime_r(&timeval
, &tm
)) {
135 printf_error("Failure in 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
= timegm(&tm
);
143 printf_error("Failure in timegm(), incorrectly formatted %s timestamp",
148 /* Get day, month, year. */
149 if (!localtime_r(&timeval
, &tm
)) {
150 printf_error("Failure in 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 printf_error("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 struct bt_notification
*evaluate_event_notification(
178 struct bt_notification
*notification
,
179 struct trimmer_iterator
*trim_it
,
180 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
181 bool *_event_in_range
)
185 struct bt_ctf_event
*event
= NULL
, *writer_event
;
186 bool in_range
= true;
187 struct bt_ctf_clock_class
*clock_class
= NULL
;
188 struct bt_ctf_trace
*trace
= NULL
;
189 struct bt_ctf_stream
*stream
= NULL
;
190 struct bt_ctf_stream_class
*stream_class
= NULL
;
191 struct bt_ctf_clock_value
*clock_value
= NULL
;
192 bool lazy_update
= false;
193 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(
201 writer_event
= trimmer_output_event(trim_it
, event
);
202 assert(writer_event
);
203 new_notification
= bt_notification_event_create(writer_event
, cc_prio_map
);
204 assert(new_notification
);
207 stream
= bt_ctf_event_get_stream(event
);
210 stream_class
= bt_ctf_stream_get_class(stream
);
211 assert(stream_class
);
213 trace
= bt_ctf_stream_class_get_trace(stream_class
);
216 /* FIXME multi-clock? */
217 clock_class
= bt_ctf_trace_get_clock_class(trace
, 0);
222 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
224 printf_error("Failed to retrieve clock value");
228 clock_ret
= bt_ctf_clock_value_get_value_ns_from_epoch(
231 printf_error("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 printf_error("Unexpected: time range begin value is above end value");
246 if (begin
->set
&& ts
< begin
->value
) {
249 if (end
->set
&& ts
> end
->value
) {
256 BT_PUT(new_notification
);
259 bt_put(writer_event
);
263 bt_put(stream_class
);
265 *_event_in_range
= in_range
;
266 return new_notification
;
270 int ns_from_integer_field(struct bt_ctf_field
*integer
, int64_t *ns
)
274 uint64_t raw_clock_value
;
275 struct bt_ctf_field_type
*integer_type
= NULL
;
276 struct bt_ctf_clock_class
*clock_class
= NULL
;
277 struct bt_ctf_clock_value
*clock_value
= NULL
;
279 integer_type
= bt_ctf_field_get_type(integer
);
280 assert(integer_type
);
281 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
288 is_signed
= bt_ctf_field_type_integer_get_signed(integer_type
);
290 ret
= bt_ctf_field_unsigned_integer_get_value(integer
,
296 /* Signed clock values are unsupported. */
300 clock_value
= bt_ctf_clock_value_create(clock_class
, raw_clock_value
);
305 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
, ns
);
307 bt_put(integer_type
);
313 static uint64_t ns_from_value(uint64_t frequency
, uint64_t value
)
317 if (frequency
== NSEC_PER_SEC
) {
320 ns
= (uint64_t) ((1e9
* (double) value
) / (double) frequency
);
327 * timestamp minus the offset.
330 int64_t get_raw_timestamp(struct bt_ctf_packet
*writer_packet
,
333 struct bt_ctf_clock_class
*writer_clock_class
;
334 int64_t sec_offset
, cycles_offset
, ns
;
335 struct bt_ctf_trace
*writer_trace
;
336 struct bt_ctf_stream
*writer_stream
;
337 struct bt_ctf_stream_class
*writer_stream_class
;
341 writer_stream
= bt_ctf_packet_get_stream(writer_packet
);
342 assert(writer_stream
);
344 writer_stream_class
= bt_ctf_stream_get_class(writer_stream
);
345 assert(writer_stream_class
);
347 writer_trace
= bt_ctf_stream_class_get_trace(writer_stream_class
);
348 assert(writer_trace
);
350 /* FIXME multi-clock? */
351 writer_clock_class
= bt_ctf_trace_get_clock_class(writer_trace
, 0);
352 assert(writer_clock_class
);
354 ret
= bt_ctf_clock_class_get_offset_s(writer_clock_class
, &sec_offset
);
356 ns
= sec_offset
* NSEC_PER_SEC
;
358 freq
= bt_ctf_clock_class_get_frequency(writer_clock_class
);
359 assert(freq
!= -1ULL);
361 ret
= bt_ctf_clock_class_get_offset_cycles(writer_clock_class
, &cycles_offset
);
364 ns
+= ns_from_value(freq
, cycles_offset
);
366 bt_put(writer_clock_class
);
367 bt_put(writer_trace
);
368 bt_put(writer_stream_class
);
369 bt_put(writer_stream
);
371 return timestamp
- ns
;
375 struct bt_notification
*evaluate_packet_notification(
376 struct bt_notification
*notification
,
377 struct trimmer_iterator
*trim_it
,
378 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
379 bool *_packet_in_range
)
381 int64_t begin_ns
, pkt_begin_ns
, end_ns
, pkt_end_ns
;
382 bool in_range
= true;
383 struct bt_ctf_packet
*packet
= NULL
, *writer_packet
= NULL
;
384 struct bt_ctf_field
*packet_context
= NULL
,
385 *timestamp_begin
= NULL
,
386 *timestamp_end
= NULL
;
387 struct bt_notification
*new_notification
= NULL
;
388 enum bt_component_status ret
;
389 bool lazy_update
= false;
391 switch (bt_notification_get_type(notification
)) {
392 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
393 packet
= bt_notification_packet_begin_get_packet(notification
);
395 writer_packet
= trimmer_new_packet(trim_it
, packet
);
396 assert(writer_packet
);
398 case BT_NOTIFICATION_TYPE_PACKET_END
:
399 packet
= bt_notification_packet_end_get_packet(notification
);
401 writer_packet
= trimmer_close_packet(trim_it
, packet
);
402 assert(writer_packet
);
408 packet_context
= bt_ctf_packet_get_context(writer_packet
);
409 if (!packet_context
) {
413 if (!bt_ctf_field_is_structure(packet_context
)) {
417 timestamp_begin
= bt_ctf_field_structure_get_field(
418 packet_context
, "timestamp_begin");
419 if (!timestamp_begin
|| !bt_ctf_field_is_integer(timestamp_begin
)) {
422 timestamp_end
= bt_ctf_field_structure_get_field(
423 packet_context
, "timestamp_end");
424 if (!timestamp_end
|| !bt_ctf_field_is_integer(timestamp_end
)) {
428 if (ns_from_integer_field(timestamp_begin
, &pkt_begin_ns
)) {
431 if (ns_from_integer_field(timestamp_end
, &pkt_end_ns
)) {
435 if (update_lazy_bound(begin
, "begin", pkt_begin_ns
, &lazy_update
)) {
438 if (update_lazy_bound(end
, "end", pkt_end_ns
, &lazy_update
)) {
441 if (lazy_update
&& begin
->set
&& end
->set
) {
442 if (begin
->value
> end
->value
) {
443 printf_error("Unexpected: time range begin value is above end value");
448 begin_ns
= begin
->set
? begin
->value
: INT64_MIN
;
449 end_ns
= end
->set
? end
->value
: INT64_MAX
;
452 * Accept if there is any overlap between the selected region and the
455 in_range
= (pkt_end_ns
>= begin_ns
) && (pkt_begin_ns
<= end_ns
);
460 if (begin_ns
> pkt_begin_ns
) {
461 ret
= update_packet_context_field(trim_it
->err
, writer_packet
,
463 get_raw_timestamp(writer_packet
, begin_ns
));
467 if (end_ns
< pkt_end_ns
) {
468 ret
= update_packet_context_field(trim_it
->err
, writer_packet
,
470 get_raw_timestamp(writer_packet
, end_ns
));
475 switch (bt_notification_get_type(notification
)) {
476 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
477 new_notification
= bt_notification_packet_begin_create(writer_packet
);
478 assert(new_notification
);
480 case BT_NOTIFICATION_TYPE_PACKET_END
:
481 new_notification
= bt_notification_packet_end_create(writer_packet
);
482 assert(new_notification
);
488 *_packet_in_range
= in_range
;
490 bt_put(writer_packet
);
491 bt_put(packet_context
);
492 bt_put(timestamp_begin
);
493 bt_put(timestamp_end
);
494 return new_notification
;
498 struct bt_notification
*evaluate_stream_notification(
499 struct bt_notification
*notification
,
500 struct trimmer_iterator
*trim_it
)
502 struct bt_ctf_stream
*stream
;
504 stream
= bt_notification_stream_end_get_stream(notification
);
507 /* FIXME: useless copy */
508 return bt_notification_stream_end_create(stream
);
511 /* Return true if the notification should be forwarded. */
513 enum bt_notification_iterator_status
evaluate_notification(
514 struct bt_notification
**notification
,
515 struct trimmer_iterator
*trim_it
,
516 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
519 enum bt_notification_type type
;
520 struct bt_notification
*new_notification
= NULL
;
523 type
= bt_notification_get_type(*notification
);
525 case BT_NOTIFICATION_TYPE_EVENT
:
526 new_notification
= evaluate_event_notification(*notification
,
527 trim_it
, begin
, end
, in_range
);
529 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
530 case BT_NOTIFICATION_TYPE_PACKET_END
:
531 new_notification
= evaluate_packet_notification(*notification
,
532 trim_it
, begin
, end
, in_range
);
534 case BT_NOTIFICATION_TYPE_STREAM_END
:
535 new_notification
= evaluate_stream_notification(*notification
,
539 /* Accept all other notifications. */
542 BT_PUT(*notification
);
543 *notification
= new_notification
;
545 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
549 struct bt_notification_iterator_next_return
trimmer_iterator_next(
550 struct bt_private_notification_iterator
*iterator
)
552 struct trimmer_iterator
*trim_it
= NULL
;
553 struct bt_private_component
*component
= NULL
;
554 struct trimmer
*trimmer
= NULL
;
555 struct bt_notification_iterator
*source_it
= NULL
;
556 struct bt_notification_iterator_next_return ret
= {
557 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
558 .notification
= NULL
,
560 bool notification_in_range
= false;
562 trim_it
= bt_private_notification_iterator_get_user_data(iterator
);
565 component
= bt_private_notification_iterator_get_private_component(
568 trimmer
= bt_private_component_get_user_data(component
);
571 source_it
= trim_it
->input_iterator
;
574 while (!notification_in_range
) {
575 ret
.status
= bt_notification_iterator_next(source_it
);
576 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
580 ret
.notification
= bt_notification_iterator_get_notification(
582 if (!ret
.notification
) {
583 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
587 ret
.status
= evaluate_notification(&ret
.notification
, trim_it
,
588 &trimmer
->begin
, &trimmer
->end
,
589 ¬ification_in_range
);
590 if (!notification_in_range
) {
591 BT_PUT(ret
.notification
);
594 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
604 enum bt_notification_iterator_status
trimmer_iterator_seek_time(
605 struct bt_private_notification_iterator
*iterator
,
608 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;