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);
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(
97 connection
= bt_private_port_get_private_connection(input_port
);
100 it_data
->input_iterator
=
101 bt_private_connection_create_notification_iterator(connection
,
103 if (!it_data
->input_iterator
) {
104 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
108 it_data
->err
= stderr
;
109 it_data
->packet_map
= g_hash_table_new_full(g_direct_hash
,
110 g_direct_equal
, NULL
, NULL
);
112 it_ret
= bt_private_notification_iterator_set_user_data(iterator
,
125 int update_lazy_bound(struct trimmer_bound
*bound
, const char *name
,
126 int64_t ts
, bool *lazy_update
)
132 *lazy_update
= false;
138 timeval
= ts
/ NSEC_PER_SEC
;
140 if (bound
->lazy_values
.gmt
) {
141 /* Get day, month, year. */
142 if (!gmtime_r(&timeval
, &tm
)) {
143 printf_error("Failure in gmtime_r()");
146 tm
.tm_sec
= bound
->lazy_values
.ss
;
147 tm
.tm_min
= bound
->lazy_values
.mm
;
148 tm
.tm_hour
= bound
->lazy_values
.hh
;
149 timeval
= timegm(&tm
);
151 printf_error("Failure in timegm(), incorrectly formatted %s timestamp",
156 /* Get day, month, year. */
157 if (!localtime_r(&timeval
, &tm
)) {
158 printf_error("Failure in localtime_r()");
161 tm
.tm_sec
= bound
->lazy_values
.ss
;
162 tm
.tm_min
= bound
->lazy_values
.mm
;
163 tm
.tm_hour
= bound
->lazy_values
.hh
;
164 timeval
= mktime(&tm
);
166 printf_error("Failure in mktime(), incorrectly formatted %s timestamp",
171 value
= (int64_t) timeval
;
172 value
*= NSEC_PER_SEC
;
173 value
+= bound
->lazy_values
.ns
;
174 bound
->value
= value
;
185 struct bt_notification
*evaluate_event_notification(
186 struct bt_notification
*notification
,
187 struct trimmer_iterator
*trim_it
,
188 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
189 bool *_event_in_range
)
193 struct bt_ctf_event
*event
= NULL
, *writer_event
;
194 bool in_range
= true;
195 struct bt_ctf_clock_class
*clock_class
= NULL
;
196 struct bt_ctf_trace
*trace
= NULL
;
197 struct bt_ctf_stream
*stream
= NULL
;
198 struct bt_ctf_stream_class
*stream_class
= NULL
;
199 struct bt_ctf_clock_value
*clock_value
= NULL
;
200 bool lazy_update
= false;
201 struct bt_notification
*new_notification
= NULL
;
202 struct bt_clock_class_priority_map
*cc_prio_map
;
204 event
= bt_notification_event_get_event(notification
);
206 cc_prio_map
= bt_notification_event_get_clock_class_priority_map(
209 writer_event
= trimmer_output_event(trim_it
, event
);
210 assert(writer_event
);
211 new_notification
= bt_notification_event_create(writer_event
, cc_prio_map
);
212 assert(new_notification
);
215 stream
= bt_ctf_event_get_stream(event
);
218 stream_class
= bt_ctf_stream_get_class(stream
);
219 assert(stream_class
);
221 trace
= bt_ctf_stream_class_get_trace(stream_class
);
224 /* FIXME multi-clock? */
225 clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, 0);
230 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
232 printf_error("Failed to retrieve clock value");
236 clock_ret
= bt_ctf_clock_value_get_value_ns_from_epoch(
239 printf_error("Failed to retrieve clock value timestamp");
242 if (update_lazy_bound(begin
, "begin", ts
, &lazy_update
)) {
245 if (update_lazy_bound(end
, "end", ts
, &lazy_update
)) {
248 if (lazy_update
&& begin
->set
&& end
->set
) {
249 if (begin
->value
> end
->value
) {
250 printf_error("Unexpected: time range begin value is above end value");
254 if (begin
->set
&& ts
< begin
->value
) {
257 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_ctf_field
*integer
, int64_t *ns
)
282 uint64_t raw_clock_value
;
283 struct bt_ctf_field_type
*integer_type
= NULL
;
284 struct bt_ctf_clock_class
*clock_class
= NULL
;
285 struct bt_ctf_clock_value
*clock_value
= NULL
;
287 integer_type
= bt_ctf_field_get_type(integer
);
288 assert(integer_type
);
289 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
296 is_signed
= bt_ctf_field_type_integer_get_signed(integer_type
);
298 ret
= bt_ctf_field_unsigned_integer_get_value(integer
,
304 /* Signed clock values are unsupported. */
308 clock_value
= bt_ctf_clock_value_create(clock_class
, raw_clock_value
);
313 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
, ns
);
315 bt_put(integer_type
);
321 static uint64_t ns_from_value(uint64_t frequency
, uint64_t value
)
325 if (frequency
== NSEC_PER_SEC
) {
328 ns
= (uint64_t) ((1e9
* (double) value
) / (double) frequency
);
335 * timestamp minus the offset.
338 int64_t get_raw_timestamp(struct bt_ctf_packet
*writer_packet
,
341 struct bt_ctf_clock_class
*writer_clock_class
;
342 int64_t sec_offset
, cycles_offset
, ns
;
343 struct bt_ctf_trace
*writer_trace
;
344 struct bt_ctf_stream
*writer_stream
;
345 struct bt_ctf_stream_class
*writer_stream_class
;
349 writer_stream
= bt_ctf_packet_get_stream(writer_packet
);
350 assert(writer_stream
);
352 writer_stream_class
= bt_ctf_stream_get_class(writer_stream
);
353 assert(writer_stream_class
);
355 writer_trace
= bt_ctf_stream_class_get_trace(writer_stream_class
);
356 assert(writer_trace
);
358 /* FIXME multi-clock? */
359 writer_clock_class
= bt_ctf_trace_get_clock_class_by_index(
361 assert(writer_clock_class
);
363 ret
= bt_ctf_clock_class_get_offset_s(writer_clock_class
, &sec_offset
);
365 ns
= sec_offset
* NSEC_PER_SEC
;
367 freq
= bt_ctf_clock_class_get_frequency(writer_clock_class
);
368 assert(freq
!= -1ULL);
370 ret
= bt_ctf_clock_class_get_offset_cycles(writer_clock_class
, &cycles_offset
);
373 ns
+= ns_from_value(freq
, cycles_offset
);
375 bt_put(writer_clock_class
);
376 bt_put(writer_trace
);
377 bt_put(writer_stream_class
);
378 bt_put(writer_stream
);
380 return timestamp
- ns
;
384 struct bt_notification
*evaluate_packet_notification(
385 struct bt_notification
*notification
,
386 struct trimmer_iterator
*trim_it
,
387 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
388 bool *_packet_in_range
)
390 int64_t begin_ns
, pkt_begin_ns
, end_ns
, pkt_end_ns
;
391 bool in_range
= true;
392 struct bt_ctf_packet
*packet
= NULL
, *writer_packet
= NULL
;
393 struct bt_ctf_field
*packet_context
= NULL
,
394 *timestamp_begin
= NULL
,
395 *timestamp_end
= NULL
;
396 struct bt_notification
*new_notification
= NULL
;
397 enum bt_component_status ret
;
398 bool lazy_update
= false;
400 switch (bt_notification_get_type(notification
)) {
401 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
402 packet
= bt_notification_packet_begin_get_packet(notification
);
404 writer_packet
= trimmer_new_packet(trim_it
, packet
);
405 assert(writer_packet
);
407 case BT_NOTIFICATION_TYPE_PACKET_END
:
408 packet
= bt_notification_packet_end_get_packet(notification
);
410 writer_packet
= trimmer_close_packet(trim_it
, packet
);
411 assert(writer_packet
);
417 packet_context
= bt_ctf_packet_get_context(writer_packet
);
418 if (!packet_context
) {
422 if (!bt_ctf_field_is_structure(packet_context
)) {
426 timestamp_begin
= bt_ctf_field_structure_get_field(
427 packet_context
, "timestamp_begin");
428 if (!timestamp_begin
|| !bt_ctf_field_is_integer(timestamp_begin
)) {
431 timestamp_end
= bt_ctf_field_structure_get_field(
432 packet_context
, "timestamp_end");
433 if (!timestamp_end
|| !bt_ctf_field_is_integer(timestamp_end
)) {
437 if (ns_from_integer_field(timestamp_begin
, &pkt_begin_ns
)) {
440 if (ns_from_integer_field(timestamp_end
, &pkt_end_ns
)) {
444 if (update_lazy_bound(begin
, "begin", pkt_begin_ns
, &lazy_update
)) {
447 if (update_lazy_bound(end
, "end", pkt_end_ns
, &lazy_update
)) {
450 if (lazy_update
&& begin
->set
&& end
->set
) {
451 if (begin
->value
> end
->value
) {
452 printf_error("Unexpected: time range begin value is above end value");
457 begin_ns
= begin
->set
? begin
->value
: INT64_MIN
;
458 end_ns
= end
->set
? end
->value
: INT64_MAX
;
461 * Accept if there is any overlap between the selected region and the
464 in_range
= (pkt_end_ns
>= begin_ns
) && (pkt_begin_ns
<= end_ns
);
469 if (begin_ns
> pkt_begin_ns
) {
470 ret
= update_packet_context_field(trim_it
->err
, writer_packet
,
472 get_raw_timestamp(writer_packet
, begin_ns
));
476 if (end_ns
< pkt_end_ns
) {
477 ret
= update_packet_context_field(trim_it
->err
, writer_packet
,
479 get_raw_timestamp(writer_packet
, end_ns
));
484 switch (bt_notification_get_type(notification
)) {
485 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
486 new_notification
= bt_notification_packet_begin_create(writer_packet
);
487 assert(new_notification
);
489 case BT_NOTIFICATION_TYPE_PACKET_END
:
490 new_notification
= bt_notification_packet_end_create(writer_packet
);
491 assert(new_notification
);
497 *_packet_in_range
= in_range
;
499 bt_put(writer_packet
);
500 bt_put(packet_context
);
501 bt_put(timestamp_begin
);
502 bt_put(timestamp_end
);
503 return new_notification
;
507 struct bt_notification
*evaluate_stream_notification(
508 struct bt_notification
*notification
,
509 struct trimmer_iterator
*trim_it
)
511 struct bt_ctf_stream
*stream
;
513 stream
= bt_notification_stream_end_get_stream(notification
);
516 /* FIXME: useless copy */
517 return bt_notification_stream_end_create(stream
);
520 /* Return true if the notification should be forwarded. */
522 enum bt_notification_iterator_status
evaluate_notification(
523 struct bt_notification
**notification
,
524 struct trimmer_iterator
*trim_it
,
525 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
528 enum bt_notification_type type
;
529 struct bt_notification
*new_notification
= NULL
;
532 type
= bt_notification_get_type(*notification
);
534 case BT_NOTIFICATION_TYPE_EVENT
:
535 new_notification
= evaluate_event_notification(*notification
,
536 trim_it
, begin
, end
, in_range
);
538 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
539 case BT_NOTIFICATION_TYPE_PACKET_END
:
540 new_notification
= evaluate_packet_notification(*notification
,
541 trim_it
, begin
, end
, in_range
);
543 case BT_NOTIFICATION_TYPE_STREAM_END
:
544 new_notification
= evaluate_stream_notification(*notification
,
548 puts("Unhandled notification type");
551 BT_PUT(*notification
);
552 *notification
= new_notification
;
554 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
558 struct bt_notification_iterator_next_return
trimmer_iterator_next(
559 struct bt_private_notification_iterator
*iterator
)
561 struct trimmer_iterator
*trim_it
= NULL
;
562 struct bt_private_component
*component
= NULL
;
563 struct trimmer
*trimmer
= NULL
;
564 struct bt_notification_iterator
*source_it
= NULL
;
565 struct bt_notification_iterator_next_return ret
= {
566 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
567 .notification
= NULL
,
569 bool notification_in_range
= false;
571 trim_it
= bt_private_notification_iterator_get_user_data(iterator
);
574 component
= bt_private_notification_iterator_get_private_component(
577 trimmer
= bt_private_component_get_user_data(component
);
580 source_it
= trim_it
->input_iterator
;
583 while (!notification_in_range
) {
584 ret
.status
= bt_notification_iterator_next(source_it
);
585 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
589 ret
.notification
= bt_notification_iterator_get_notification(
591 if (!ret
.notification
) {
592 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
596 ret
.status
= evaluate_notification(&ret
.notification
, trim_it
,
597 &trimmer
->begin
, &trimmer
->end
,
598 ¬ification_in_range
);
599 if (!notification_in_range
) {
600 BT_PUT(ret
.notification
);
603 if (ret
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
613 enum bt_notification_iterator_status
trimmer_iterator_seek_time(
614 struct bt_private_notification_iterator
*iterator
,
617 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;