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/plugin/notification/iterator.h>
32 #include <babeltrace/plugin/notification/notification.h>
33 #include <babeltrace/plugin/notification/event.h>
34 #include <babeltrace/plugin/notification/stream.h>
35 #include <babeltrace/plugin/notification/packet.h>
36 #include <babeltrace/plugin/filter.h>
37 #include <babeltrace/ctf-ir/event.h>
38 #include <babeltrace/ctf-ir/stream.h>
39 #include <babeltrace/ctf-ir/stream-class.h>
40 #include <babeltrace/ctf-ir/clock.h>
41 #include <babeltrace/ctf-ir/packet.h>
42 #include <babeltrace/ctf-ir/trace.h>
43 #include <babeltrace/ctf-ir/fields.h>
47 void trimmer_iterator_destroy(struct bt_notification_iterator
*it
)
49 struct trimmer_iterator
*it_data
;
51 it_data
= bt_notification_iterator_get_private_data(it
);
54 if (it_data
->input_iterator_group
) {
55 g_ptr_array_free(it_data
->input_iterator_group
, TRUE
);
57 bt_put(it_data
->current_notification
);
62 enum bt_component_status
trimmer_iterator_init(struct bt_component
*component
,
63 struct bt_notification_iterator
*iterator
)
65 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
66 enum bt_notification_iterator_status it_ret
;
67 struct trimmer_iterator
*it_data
= g_new0(struct trimmer_iterator
, 1);
70 ret
= BT_COMPONENT_STATUS_NOMEM
;
74 /* FIXME init trimmer_iterator */
75 it_ret
= bt_notification_iterator_set_private_data(iterator
, it_data
);
80 it_ret
= bt_notification_iterator_set_destroy_cb(iterator
,
81 trimmer_iterator_destroy
);
83 ret
= BT_COMPONENT_STATUS_ERROR
;
87 it_ret
= bt_notification_iterator_set_next_cb(iterator
,
88 trimmer_iterator_next
);
90 ret
= BT_COMPONENT_STATUS_ERROR
;
94 it_ret
= bt_notification_iterator_set_get_cb(iterator
,
95 trimmer_iterator_get
);
97 ret
= BT_COMPONENT_STATUS_ERROR
;
101 it_ret
= bt_notification_iterator_set_seek_time_cb(iterator
,
102 trimmer_iterator_seek_time
);
104 ret
= BT_COMPONENT_STATUS_ERROR
;
112 struct bt_notification
*trimmer_iterator_get(
113 struct bt_notification_iterator
*iterator
)
115 struct trimmer_iterator
*trim_it
;
117 trim_it
= bt_notification_iterator_get_private_data(iterator
);
120 if (!trim_it
->current_notification
) {
121 enum bt_notification_iterator_status it_ret
;
123 it_ret
= trimmer_iterator_next(iterator
);
129 return bt_get(trim_it
->current_notification
);
133 int update_lazy_bound(struct trimmer_bound
*bound
, const char *name
,
134 int64_t ts
, bool *lazy_update
)
140 *lazy_update
= false;
146 timeval
= ts
/ NSEC_PER_SEC
;
148 if (bound
->lazy_values
.gmt
) {
149 /* Get day, month, year. */
150 if (!gmtime_r(&timeval
, &tm
)) {
151 printf_error("Failure in gmtime_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
= timegm(&tm
);
159 printf_error("Failure in timegm(), incorrectly formatted %s timestamp",
164 /* Get day, month, year. */
165 if (!localtime_r(&timeval
, &tm
)) {
166 printf_error("Failure in localtime_r()");
169 tm
.tm_sec
= bound
->lazy_values
.ss
;
170 tm
.tm_min
= bound
->lazy_values
.mm
;
171 tm
.tm_hour
= bound
->lazy_values
.hh
;
172 timeval
= mktime(&tm
);
174 printf_error("Failure in mktime(), incorrectly formatted %s timestamp",
179 value
= (int64_t) timeval
;
180 value
*= NSEC_PER_SEC
;
181 value
+= bound
->lazy_values
.ns
;
182 bound
->value
= value
;
193 enum bt_notification_iterator_status
194 evaluate_event_notification(struct bt_notification
*notification
,
195 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
196 bool *_event_in_range
)
200 struct bt_ctf_event
*event
= NULL
;
201 bool in_range
= true;
202 struct bt_ctf_clock
*clock
= NULL
;
203 struct bt_ctf_trace
*trace
= NULL
;
204 struct bt_ctf_stream
*stream
= NULL
;
205 struct bt_ctf_stream_class
*stream_class
= NULL
;
206 struct bt_ctf_clock_value
*clock_value
= NULL
;
207 enum bt_notification_iterator_status ret
=
208 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
209 bool lazy_update
= false;
211 event
= bt_notification_event_get_event(notification
);
214 stream
= bt_ctf_event_get_stream(event
);
217 stream_class
= bt_ctf_stream_get_class(stream
);
218 assert(stream_class
);
220 trace
= bt_ctf_stream_class_get_trace(stream_class
);
223 /* FIXME multi-clock? */
224 clock
= bt_ctf_trace_get_clock(trace
, 0);
229 clock_value
= bt_ctf_event_get_clock_value(event
, clock
);
231 printf_error("Failed to retrieve clock value");
232 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
236 clock_ret
= bt_ctf_clock_value_get_value_ns_from_epoch(
239 printf_error("Failed to retrieve clock value timestamp");
240 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
243 if (update_lazy_bound(begin
, "begin", ts
, &lazy_update
)) {
244 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
247 if (update_lazy_bound(end
, "end", ts
, &lazy_update
)) {
248 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
251 if (lazy_update
&& begin
->set
&& end
->set
) {
252 if (begin
->value
> end
->value
) {
253 printf_error("Unexpected: time range begin value is above end value");
254 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
258 if (begin
->set
&& ts
< begin
->value
) {
261 if (end
->set
&& ts
> end
->value
) {
269 bt_put(stream_class
);
271 *_event_in_range
= in_range
;
276 int ns_from_integer_field(struct bt_ctf_field
*integer
, int64_t *ns
)
280 uint64_t raw_clock_value
;
281 struct bt_ctf_field_type
*integer_type
= NULL
;
282 struct bt_ctf_clock
*clock
= NULL
;
283 struct bt_ctf_clock_value
*clock_value
= NULL
;
285 integer_type
= bt_ctf_field_get_type(integer
);
286 assert(integer_type
);
287 clock
= bt_ctf_field_type_integer_get_mapped_clock(integer_type
);
293 is_signed
= bt_ctf_field_type_integer_get_signed(integer_type
);
295 ret
= bt_ctf_field_unsigned_integer_get_value(integer
,
301 /* Signed clock values are unsupported. */
305 clock_value
= bt_ctf_clock_value_create(clock
, raw_clock_value
);
310 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
, ns
);
312 bt_put(integer_type
);
319 enum bt_notification_iterator_status
evaluate_packet_notification(
320 struct bt_notification
*notification
,
321 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
322 bool *_packet_in_range
)
324 enum bt_notification_iterator_status ret
=
325 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
326 int64_t begin_ns
, pkt_begin_ns
, end_ns
, pkt_end_ns
;
327 bool in_range
= true;
328 struct bt_ctf_packet
*packet
= NULL
;
329 struct bt_ctf_field
*packet_context
= NULL
,
330 *timestamp_begin
= NULL
,
331 *timestamp_end
= NULL
;
333 switch (bt_notification_get_type(notification
)) {
334 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
335 packet
= bt_notification_packet_begin_get_packet(notification
);
337 case BT_NOTIFICATION_TYPE_PACKET_END
:
338 packet
= bt_notification_packet_end_get_packet(notification
);
345 packet_context
= bt_ctf_packet_get_context(packet
);
346 if (!packet_context
) {
350 if (!bt_ctf_field_is_structure(packet_context
)) {
354 timestamp_begin
= bt_ctf_field_structure_get_field(
355 packet_context
, "timestamp_begin");
356 if (!timestamp_begin
|| !bt_ctf_field_is_integer(timestamp_begin
)) {
359 timestamp_end
= bt_ctf_field_structure_get_field(
360 packet_context
, "timestamp_end");
361 if (!timestamp_end
|| !bt_ctf_field_is_integer(timestamp_end
)) {
365 if (ns_from_integer_field(timestamp_begin
, &pkt_begin_ns
)) {
368 if (ns_from_integer_field(timestamp_end
, &pkt_end_ns
)) {
372 begin_ns
= begin
->set
? begin
->value
: INT64_MIN
;
373 end_ns
= end
->set
? end
->value
: INT64_MAX
;
376 * Accept if there is any overlap between the selected region and the
379 in_range
= (pkt_end_ns
>= begin_ns
) && (pkt_begin_ns
<= end_ns
);
381 *_packet_in_range
= in_range
;
383 bt_put(packet_context
);
384 bt_put(timestamp_begin
);
385 bt_put(timestamp_end
);
389 /* Return true if the notification should be forwarded. */
391 enum bt_notification_iterator_status
evaluate_notification(
392 struct bt_notification
*notification
,
393 struct trimmer_bound
*begin
, struct trimmer_bound
*end
,
396 enum bt_notification_type type
;
397 enum bt_notification_iterator_status ret
=
398 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
400 type
= bt_notification_get_type(notification
);
402 case BT_NOTIFICATION_TYPE_EVENT
:
403 ret
= evaluate_event_notification(notification
, begin
,
406 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
407 case BT_NOTIFICATION_TYPE_PACKET_END
:
408 ret
= evaluate_packet_notification(notification
, begin
,
412 /* Accept all other notifications. */
419 enum bt_notification_iterator_status
trimmer_iterator_next(
420 struct bt_notification_iterator
*iterator
)
422 struct trimmer_iterator
*trim_it
= NULL
;
423 struct bt_component
*component
= NULL
;
424 struct trimmer
*trimmer
= NULL
;
425 struct bt_notification_iterator
*source_it
= NULL
;
426 enum bt_notification_iterator_status ret
=
427 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
428 enum bt_component_status component_ret
;
429 bool notification_in_range
= false;
431 trim_it
= bt_notification_iterator_get_private_data(iterator
);
434 component
= bt_notification_iterator_get_component(iterator
);
436 trimmer
= bt_component_get_private_data(component
);
439 /* FIXME, should handle input iterator groups. */
440 component_ret
= bt_component_filter_get_input_iterator(component
, 0,
442 assert((component_ret
== BT_COMPONENT_STATUS_OK
) && source_it
);
444 while (!notification_in_range
) {
445 struct bt_notification
*notification
;
447 ret
= bt_notification_iterator_next(source_it
);
448 if (ret
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
452 notification
= bt_notification_iterator_get_notification(
455 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
459 ret
= evaluate_notification(notification
,
460 &trimmer
->begin
, &trimmer
->end
,
461 ¬ification_in_range
);
462 if (notification_in_range
) {
463 BT_MOVE(trim_it
->current_notification
, notification
);
465 bt_put(notification
);
475 enum bt_notification_iterator_status
trimmer_iterator_seek_time(
476 struct bt_notification_iterator
*iterator
, int64_t time
)
478 enum bt_notification_iterator_status ret
;
480 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;