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
31 #include <babeltrace/component/notification/iterator.h>
32 #include <babeltrace/component/notification/private-iterator.h>
33 #include <babeltrace/component/notification/notification.h>
34 #include <babeltrace/component/notification/event.h>
35 #include <babeltrace/component/notification/stream.h>
36 #include <babeltrace/component/notification/packet.h>
37 #include <babeltrace/component/component-filter.h>
38 #include <babeltrace/component/private-component-filter.h>
39 #include <babeltrace/component/private-port.h>
40 #include <babeltrace/component/private-connection.h>
41 #include <babeltrace/component/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>
53 void trimmer_iterator_destroy(struct bt_private_notification_iterator
*it
)
55 struct trimmer_iterator
*it_data
;
57 it_data
= bt_private_notification_iterator_get_user_data(it
);
60 bt_put(it_data
->current_notification
);
61 bt_put(it_data
->input_iterator
);
66 enum bt_notification_iterator_status
trimmer_iterator_init(
67 struct bt_private_component
*component
,
68 struct bt_private_port
*port
,
69 struct bt_private_notification_iterator
*iterator
)
71 enum bt_notification_iterator_status ret
=
72 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
73 enum bt_notification_iterator_status it_ret
;
74 struct bt_private_port
*input_port
= NULL
;
75 struct bt_private_connection
*connection
= NULL
;
76 struct trimmer_iterator
*it_data
= g_new0(struct trimmer_iterator
, 1);
79 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
83 /* Create a new iterator on the upstream component. */
84 input_port
= bt_private_component_filter_get_default_input_private_port(
87 connection
= bt_private_port_get_private_connection(input_port
);
90 it_data
->input_iterator
=
91 bt_private_connection_create_notification_iterator(connection
);
92 if (!it_data
->input_iterator
) {
93 ret
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
97 it_ret
= bt_private_notification_iterator_set_user_data(iterator
,
109 struct bt_notification
*trimmer_iterator_get(
110 struct bt_private_notification_iterator
*iterator
)
112 struct trimmer_iterator
*trim_it
;
114 trim_it
= bt_private_notification_iterator_get_user_data(iterator
);
117 if (!trim_it
->current_notification
) {
118 enum bt_notification_iterator_status it_ret
;
120 it_ret
= trimmer_iterator_next(iterator
);
126 return bt_get(trim_it
->current_notification
);
130 int update_lazy_bound(struct trimmer_bound
*bound
, const char *name
,
131 int64_t ts
, bool *lazy_update
)
137 *lazy_update
= false;
143 timeval
= ts
/ NSEC_PER_SEC
;
145 if (bound
->lazy_values
.gmt
) {
146 /* Get day, month, year. */
147 if (!gmtime_r(&timeval
, &tm
)) {
148 printf_error("Failure in gmtime_r()");
151 tm
.tm_sec
= bound
->lazy_values
.ss
;
152 tm
.tm_min
= bound
->lazy_values
.mm
;
153 tm
.tm_hour
= bound
->lazy_values
.hh
;
154 timeval
= timegm(&tm
);
156 printf_error("Failure in timegm(), incorrectly formatted %s timestamp",
161 /* Get day, month, year. */
162 if (!localtime_r(&timeval
, &tm
)) {
163 printf_error("Failure in localtime_r()");
166 tm
.tm_sec
= bound
->lazy_values
.ss
;
167 tm
.tm_min
= bound
->lazy_values
.mm
;
168 tm
.tm_hour
= bound
->lazy_values
.hh
;
169 timeval
= mktime(&tm
);
171 printf_error("Failure in mktime(), incorrectly formatted %s timestamp",
176 value
= (int64_t) timeval
;
177 value
*= NSEC_PER_SEC
;
178 value
+= bound
->lazy_values
.ns
;
179 bound
->value
= value
;
190 enum bt_notification_iterator_status
191 evaluate_event_notification(struct bt_notification
*notification
,
192 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
193 bool *_event_in_range
)
197 struct bt_ctf_event
*event
= NULL
;
198 bool in_range
= true;
199 struct bt_ctf_clock_class
*clock_class
= NULL
;
200 struct bt_ctf_trace
*trace
= NULL
;
201 struct bt_ctf_stream
*stream
= NULL
;
202 struct bt_ctf_stream_class
*stream_class
= NULL
;
203 struct bt_ctf_clock_value
*clock_value
= NULL
;
204 enum bt_notification_iterator_status ret
=
205 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
206 bool lazy_update
= false;
208 event
= bt_notification_event_get_event(notification
);
211 stream
= bt_ctf_event_get_stream(event
);
214 stream_class
= bt_ctf_stream_get_class(stream
);
215 assert(stream_class
);
217 trace
= bt_ctf_stream_class_get_trace(stream_class
);
220 /* FIXME multi-clock? */
221 clock_class
= bt_ctf_trace_get_clock_class(trace
, 0);
226 clock_value
= bt_ctf_event_get_clock_value(event
, clock_class
);
228 printf_error("Failed to retrieve clock value");
229 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
233 clock_ret
= bt_ctf_clock_value_get_value_ns_from_epoch(
236 printf_error("Failed to retrieve clock value timestamp");
237 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
240 if (update_lazy_bound(begin
, "begin", ts
, &lazy_update
)) {
241 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
244 if (update_lazy_bound(end
, "end", ts
, &lazy_update
)) {
245 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
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");
251 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
255 if (begin
->set
&& ts
< begin
->value
) {
258 if (end
->set
&& ts
> end
->value
) {
266 bt_put(stream_class
);
268 *_event_in_range
= in_range
;
273 int ns_from_integer_field(struct bt_ctf_field
*integer
, int64_t *ns
)
277 uint64_t raw_clock_value
;
278 struct bt_ctf_field_type
*integer_type
= NULL
;
279 struct bt_ctf_clock_class
*clock_class
= NULL
;
280 struct bt_ctf_clock_value
*clock_value
= NULL
;
282 integer_type
= bt_ctf_field_get_type(integer
);
283 assert(integer_type
);
284 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
291 is_signed
= bt_ctf_field_type_integer_get_signed(integer_type
);
293 ret
= bt_ctf_field_unsigned_integer_get_value(integer
,
299 /* Signed clock values are unsupported. */
303 clock_value
= bt_ctf_clock_value_create(clock_class
, raw_clock_value
);
308 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
, ns
);
310 bt_put(integer_type
);
317 enum bt_notification_iterator_status
evaluate_packet_notification(
318 struct bt_notification
*notification
,
319 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
320 bool *_packet_in_range
)
322 enum bt_notification_iterator_status ret
=
323 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
324 int64_t begin_ns
, pkt_begin_ns
, end_ns
, pkt_end_ns
;
325 bool in_range
= true;
326 struct bt_ctf_packet
*packet
= NULL
;
327 struct bt_ctf_field
*packet_context
= NULL
,
328 *timestamp_begin
= NULL
,
329 *timestamp_end
= NULL
;
331 switch (bt_notification_get_type(notification
)) {
332 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
333 packet
= bt_notification_packet_begin_get_packet(notification
);
335 case BT_NOTIFICATION_TYPE_PACKET_END
:
336 packet
= bt_notification_packet_end_get_packet(notification
);
343 packet_context
= bt_ctf_packet_get_context(packet
);
344 if (!packet_context
) {
348 if (!bt_ctf_field_is_structure(packet_context
)) {
352 timestamp_begin
= bt_ctf_field_structure_get_field(
353 packet_context
, "timestamp_begin");
354 if (!timestamp_begin
|| !bt_ctf_field_is_integer(timestamp_begin
)) {
357 timestamp_end
= bt_ctf_field_structure_get_field(
358 packet_context
, "timestamp_end");
359 if (!timestamp_end
|| !bt_ctf_field_is_integer(timestamp_end
)) {
363 if (ns_from_integer_field(timestamp_begin
, &pkt_begin_ns
)) {
366 if (ns_from_integer_field(timestamp_end
, &pkt_end_ns
)) {
370 begin_ns
= begin
->set
? begin
->value
: INT64_MIN
;
371 end_ns
= end
->set
? end
->value
: INT64_MAX
;
374 * Accept if there is any overlap between the selected region and the
377 in_range
= (pkt_end_ns
>= begin_ns
) && (pkt_begin_ns
<= end_ns
);
379 *_packet_in_range
= in_range
;
381 bt_put(packet_context
);
382 bt_put(timestamp_begin
);
383 bt_put(timestamp_end
);
387 /* Return true if the notification should be forwarded. */
389 enum bt_notification_iterator_status
evaluate_notification(
390 struct bt_notification
*notification
,
391 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
394 enum bt_notification_type type
;
395 enum bt_notification_iterator_status ret
=
396 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
399 type
= bt_notification_get_type(notification
);
401 case BT_NOTIFICATION_TYPE_EVENT
:
402 ret
= evaluate_event_notification(notification
, begin
,
405 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
406 case BT_NOTIFICATION_TYPE_PACKET_END
:
407 ret
= evaluate_packet_notification(notification
, begin
,
411 /* Accept all other notifications. */
418 enum bt_notification_iterator_status
trimmer_iterator_next(
419 struct bt_private_notification_iterator
*iterator
)
421 struct trimmer_iterator
*trim_it
= NULL
;
422 struct bt_private_component
*component
= NULL
;
423 struct trimmer
*trimmer
= NULL
;
424 struct bt_notification_iterator
*source_it
= NULL
;
425 enum bt_notification_iterator_status ret
=
426 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
427 bool notification_in_range
= false;
429 trim_it
= bt_private_notification_iterator_get_user_data(iterator
);
432 component
= bt_private_notification_iterator_get_private_component(
435 trimmer
= bt_private_component_get_user_data(component
);
438 source_it
= trim_it
->input_iterator
;
441 while (!notification_in_range
) {
442 struct bt_notification
*notification
;
444 ret
= bt_notification_iterator_next(source_it
);
445 if (ret
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
449 notification
= bt_notification_iterator_get_notification(
452 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
456 ret
= evaluate_notification(notification
,
457 &trimmer
->begin
, &trimmer
->end
,
458 ¬ification_in_range
);
459 if (notification_in_range
) {
460 BT_MOVE(trim_it
->current_notification
, notification
);
462 bt_put(notification
);
465 if (ret
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
475 enum bt_notification_iterator_status
trimmer_iterator_seek_time(
476 struct bt_private_notification_iterator
*iterator
,
479 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;