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_input_private_port_by_name(
90 connection
= bt_private_port_get_private_connection(input_port
);
93 it_data
->input_iterator
=
94 bt_private_connection_create_notification_iterator(connection
,
96 if (!it_data
->input_iterator
) {
97 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
101 it_data
->err
= stderr
;
102 it_data
->packet_map
= g_hash_table_new_full(g_direct_hash
,
103 g_direct_equal
, NULL
, NULL
);
105 it_ret
= bt_private_notification_iterator_set_user_data(iterator
,
118 int update_lazy_bound(struct trimmer_bound
*bound
, const char *name
,
119 int64_t ts
, bool *lazy_update
)
125 *lazy_update
= false;
131 timeval
= ts
/ NSEC_PER_SEC
;
133 if (bound
->lazy_values
.gmt
) {
134 /* Get day, month, year. */
135 if (!gmtime_r(&timeval
, &tm
)) {
136 printf_error("Failure in gmtime_r()");
139 tm
.tm_sec
= bound
->lazy_values
.ss
;
140 tm
.tm_min
= bound
->lazy_values
.mm
;
141 tm
.tm_hour
= bound
->lazy_values
.hh
;
142 timeval
= timegm(&tm
);
144 printf_error("Failure in timegm(), incorrectly formatted %s timestamp",
149 /* Get day, month, year. */
150 if (!localtime_r(&timeval
, &tm
)) {
151 printf_error("Failure in localtime_r()");
154 tm
.tm_sec
= bound
->lazy_values
.ss
;
155 tm
.tm_min
= bound
->lazy_values
.mm
;
156 tm
.tm_hour
= bound
->lazy_values
.hh
;
157 timeval
= mktime(&tm
);
159 printf_error("Failure in mktime(), incorrectly formatted %s timestamp",
164 value
= (int64_t) timeval
;
165 value
*= NSEC_PER_SEC
;
166 value
+= bound
->lazy_values
.ns
;
167 bound
->value
= value
;
178 struct bt_notification
*evaluate_event_notification(
179 struct bt_notification
*notification
,
180 struct trimmer_iterator
*trim_it
,
181 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
182 bool *_event_in_range
)
186 struct bt_ctf_event
*event
= NULL
, *writer_event
;
187 bool in_range
= true;
188 struct bt_ctf_clock_class
*clock_class
= NULL
;
189 struct bt_ctf_trace
*trace
= NULL
;
190 struct bt_ctf_stream
*stream
= NULL
;
191 struct bt_ctf_stream_class
*stream_class
= NULL
;
192 struct bt_ctf_clock_value
*clock_value
= NULL
;
193 bool lazy_update
= false;
194 struct bt_notification
*new_notification
= NULL
;
195 struct bt_clock_class_priority_map
*cc_prio_map
;
197 event
= bt_notification_event_get_event(notification
);
199 cc_prio_map
= bt_notification_event_get_clock_class_priority_map(
202 writer_event
= trimmer_output_event(trim_it
, event
);
203 assert(writer_event
);
204 new_notification
= bt_notification_event_create(writer_event
, cc_prio_map
);
205 assert(new_notification
);
208 stream
= bt_ctf_event_get_stream(event
);
211 stream_class
= bt_ctf_stream_get_class(stream
);
212 assert(stream_class
);
214 trace
= bt_ctf_stream_class_get_trace(stream_class
);
217 /* FIXME multi-clock? */
218 clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, 0);
223 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
225 printf_error("Failed to retrieve clock value");
229 clock_ret
= bt_ctf_clock_value_get_value_ns_from_epoch(
232 printf_error("Failed to retrieve clock value timestamp");
235 if (update_lazy_bound(begin
, "begin", ts
, &lazy_update
)) {
238 if (update_lazy_bound(end
, "end", ts
, &lazy_update
)) {
241 if (lazy_update
&& begin
->set
&& end
->set
) {
242 if (begin
->value
> end
->value
) {
243 printf_error("Unexpected: time range begin value is above end value");
247 if (begin
->set
&& ts
< begin
->value
) {
250 if (end
->set
&& ts
> end
->value
) {
257 BT_PUT(new_notification
);
260 bt_put(writer_event
);
264 bt_put(stream_class
);
266 *_event_in_range
= in_range
;
267 return new_notification
;
271 int ns_from_integer_field(struct bt_ctf_field
*integer
, int64_t *ns
)
275 uint64_t raw_clock_value
;
276 struct bt_ctf_field_type
*integer_type
= NULL
;
277 struct bt_ctf_clock_class
*clock_class
= NULL
;
278 struct bt_ctf_clock_value
*clock_value
= NULL
;
280 integer_type
= bt_ctf_field_get_type(integer
);
281 assert(integer_type
);
282 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
289 is_signed
= bt_ctf_field_type_integer_get_signed(integer_type
);
291 ret
= bt_ctf_field_unsigned_integer_get_value(integer
,
297 /* Signed clock values are unsupported. */
301 clock_value
= bt_ctf_clock_value_create(clock_class
, raw_clock_value
);
306 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
, ns
);
308 bt_put(integer_type
);
314 static uint64_t ns_from_value(uint64_t frequency
, uint64_t value
)
318 if (frequency
== NSEC_PER_SEC
) {
321 ns
= (uint64_t) ((1e9
* (double) value
) / (double) frequency
);
328 * timestamp minus the offset.
331 int64_t get_raw_timestamp(struct bt_ctf_packet
*writer_packet
,
334 struct bt_ctf_clock_class
*writer_clock_class
;
335 int64_t sec_offset
, cycles_offset
, ns
;
336 struct bt_ctf_trace
*writer_trace
;
337 struct bt_ctf_stream
*writer_stream
;
338 struct bt_ctf_stream_class
*writer_stream_class
;
342 writer_stream
= bt_ctf_packet_get_stream(writer_packet
);
343 assert(writer_stream
);
345 writer_stream_class
= bt_ctf_stream_get_class(writer_stream
);
346 assert(writer_stream_class
);
348 writer_trace
= bt_ctf_stream_class_get_trace(writer_stream_class
);
349 assert(writer_trace
);
351 /* FIXME multi-clock? */
352 writer_clock_class
= bt_ctf_trace_get_clock_class_by_index(
354 assert(writer_clock_class
);
356 ret
= bt_ctf_clock_class_get_offset_s(writer_clock_class
, &sec_offset
);
358 ns
= sec_offset
* NSEC_PER_SEC
;
360 freq
= bt_ctf_clock_class_get_frequency(writer_clock_class
);
361 assert(freq
!= -1ULL);
363 ret
= bt_ctf_clock_class_get_offset_cycles(writer_clock_class
, &cycles_offset
);
366 ns
+= ns_from_value(freq
, cycles_offset
);
368 bt_put(writer_clock_class
);
369 bt_put(writer_trace
);
370 bt_put(writer_stream_class
);
371 bt_put(writer_stream
);
373 return timestamp
- ns
;
377 struct bt_notification
*evaluate_packet_notification(
378 struct bt_notification
*notification
,
379 struct trimmer_iterator
*trim_it
,
380 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
381 bool *_packet_in_range
)
383 int64_t begin_ns
, pkt_begin_ns
, end_ns
, pkt_end_ns
;
384 bool in_range
= true;
385 struct bt_ctf_packet
*packet
= NULL
, *writer_packet
= NULL
;
386 struct bt_ctf_field
*packet_context
= NULL
,
387 *timestamp_begin
= NULL
,
388 *timestamp_end
= NULL
;
389 struct bt_notification
*new_notification
= NULL
;
390 enum bt_component_status ret
;
391 bool lazy_update
= false;
393 switch (bt_notification_get_type(notification
)) {
394 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
395 packet
= bt_notification_packet_begin_get_packet(notification
);
397 writer_packet
= trimmer_new_packet(trim_it
, packet
);
398 assert(writer_packet
);
400 case BT_NOTIFICATION_TYPE_PACKET_END
:
401 packet
= bt_notification_packet_end_get_packet(notification
);
403 writer_packet
= trimmer_close_packet(trim_it
, packet
);
404 assert(writer_packet
);
410 packet_context
= bt_ctf_packet_get_context(writer_packet
);
411 if (!packet_context
) {
415 if (!bt_ctf_field_is_structure(packet_context
)) {
419 timestamp_begin
= bt_ctf_field_structure_get_field(
420 packet_context
, "timestamp_begin");
421 if (!timestamp_begin
|| !bt_ctf_field_is_integer(timestamp_begin
)) {
424 timestamp_end
= bt_ctf_field_structure_get_field(
425 packet_context
, "timestamp_end");
426 if (!timestamp_end
|| !bt_ctf_field_is_integer(timestamp_end
)) {
430 if (ns_from_integer_field(timestamp_begin
, &pkt_begin_ns
)) {
433 if (ns_from_integer_field(timestamp_end
, &pkt_end_ns
)) {
437 if (update_lazy_bound(begin
, "begin", pkt_begin_ns
, &lazy_update
)) {
440 if (update_lazy_bound(end
, "end", pkt_end_ns
, &lazy_update
)) {
443 if (lazy_update
&& begin
->set
&& end
->set
) {
444 if (begin
->value
> end
->value
) {
445 printf_error("Unexpected: time range begin value is above end value");
450 begin_ns
= begin
->set
? begin
->value
: INT64_MIN
;
451 end_ns
= end
->set
? end
->value
: INT64_MAX
;
454 * Accept if there is any overlap between the selected region and the
457 in_range
= (pkt_end_ns
>= begin_ns
) && (pkt_begin_ns
<= end_ns
);
462 if (begin_ns
> pkt_begin_ns
) {
463 ret
= update_packet_context_field(trim_it
->err
, writer_packet
,
465 get_raw_timestamp(writer_packet
, begin_ns
));
469 if (end_ns
< pkt_end_ns
) {
470 ret
= update_packet_context_field(trim_it
->err
, writer_packet
,
472 get_raw_timestamp(writer_packet
, end_ns
));
477 switch (bt_notification_get_type(notification
)) {
478 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
479 new_notification
= bt_notification_packet_begin_create(writer_packet
);
480 assert(new_notification
);
482 case BT_NOTIFICATION_TYPE_PACKET_END
:
483 new_notification
= bt_notification_packet_end_create(writer_packet
);
484 assert(new_notification
);
490 *_packet_in_range
= in_range
;
492 bt_put(writer_packet
);
493 bt_put(packet_context
);
494 bt_put(timestamp_begin
);
495 bt_put(timestamp_end
);
496 return new_notification
;
500 struct bt_notification
*evaluate_stream_notification(
501 struct bt_notification
*notification
,
502 struct trimmer_iterator
*trim_it
)
504 struct bt_ctf_stream
*stream
;
506 stream
= bt_notification_stream_end_get_stream(notification
);
509 /* FIXME: useless copy */
510 return bt_notification_stream_end_create(stream
);
513 /* Return true if the notification should be forwarded. */
515 enum bt_notification_iterator_status
evaluate_notification(
516 struct bt_notification
**notification
,
517 struct trimmer_iterator
*trim_it
,
518 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
521 enum bt_notification_type type
;
522 struct bt_notification
*new_notification
= NULL
;
525 type
= bt_notification_get_type(*notification
);
527 case BT_NOTIFICATION_TYPE_EVENT
:
528 new_notification
= evaluate_event_notification(*notification
,
529 trim_it
, begin
, end
, in_range
);
531 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
532 case BT_NOTIFICATION_TYPE_PACKET_END
:
533 new_notification
= evaluate_packet_notification(*notification
,
534 trim_it
, begin
, end
, in_range
);
536 case BT_NOTIFICATION_TYPE_STREAM_END
:
537 new_notification
= evaluate_stream_notification(*notification
,
541 /* Accept all other notifications. */
544 BT_PUT(*notification
);
545 *notification
= new_notification
;
547 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
551 struct bt_notification_iterator_next_return
trimmer_iterator_next(
552 struct bt_private_notification_iterator
*iterator
)
554 struct trimmer_iterator
*trim_it
= NULL
;
555 struct bt_private_component
*component
= NULL
;
556 struct trimmer
*trimmer
= NULL
;
557 struct bt_notification_iterator
*source_it
= NULL
;
558 struct bt_notification_iterator_next_return ret
= {
559 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
560 .notification
= NULL
,
562 bool notification_in_range
= false;
564 trim_it
= bt_private_notification_iterator_get_user_data(iterator
);
567 component
= bt_private_notification_iterator_get_private_component(
570 trimmer
= bt_private_component_get_user_data(component
);
573 source_it
= trim_it
->input_iterator
;
576 while (!notification_in_range
) {
577 ret
.status
= bt_notification_iterator_next(source_it
);
578 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
582 ret
.notification
= bt_notification_iterator_get_notification(
584 if (!ret
.notification
) {
585 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
589 ret
.status
= evaluate_notification(&ret
.notification
, trim_it
,
590 &trimmer
->begin
, &trimmer
->end
,
591 ¬ification_in_range
);
592 if (!notification_in_range
) {
593 BT_PUT(ret
.notification
);
596 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
606 enum bt_notification_iterator_status
trimmer_iterator_seek_time(
607 struct bt_private_notification_iterator
*iterator
,
610 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;