trimmer: close when end is reached
[babeltrace.git] / plugins / utils / trimmer / iterator.c
1 /*
2 * iterator.c
3 *
4 * Babeltrace Trace Trimmer Iterator
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
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>
47 #include <assert.h>
48 #include <plugins-common.h>
49
50 #include "trimmer.h"
51 #include "iterator.h"
52 #include "copy.h"
53
54 BT_HIDDEN
55 void trimmer_iterator_finalize(struct bt_private_notification_iterator *it)
56 {
57 struct trimmer_iterator *it_data;
58
59 it_data = bt_private_notification_iterator_get_user_data(it);
60 assert(it_data);
61
62 bt_put(it_data->input_iterator);
63 g_hash_table_destroy(it_data->packet_map);
64 g_free(it_data);
65 }
66
67 BT_HIDDEN
68 enum bt_notification_iterator_status trimmer_iterator_init(
69 struct bt_private_notification_iterator *iterator,
70 struct bt_private_port *port)
71 {
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,
86 };
87
88 if (!it_data) {
89 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
90 goto end;
91 }
92
93 /* Create a new iterator on the upstream component. */
94 input_port = bt_private_component_filter_get_input_private_port_by_name(
95 component, "in");
96 assert(input_port);
97 connection = bt_private_port_get_private_connection(input_port);
98 assert(connection);
99
100 it_data->input_iterator =
101 bt_private_connection_create_notification_iterator(connection,
102 notif_types);
103 if (!it_data->input_iterator) {
104 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
105 goto end;
106 }
107
108 it_data->err = stderr;
109 it_data->packet_map = g_hash_table_new_full(g_direct_hash,
110 g_direct_equal, NULL, NULL);
111
112 it_ret = bt_private_notification_iterator_set_user_data(iterator,
113 it_data);
114 if (it_ret) {
115 goto end;
116 }
117 end:
118 bt_put(component);
119 bt_put(connection);
120 bt_put(input_port);
121 return ret;
122 }
123
124 static
125 int update_lazy_bound(struct trimmer_bound *bound, const char *name,
126 int64_t ts, bool *lazy_update)
127 {
128 struct tm tm;
129 int64_t value;
130 time_t timeval;
131
132 *lazy_update = false;
133
134 if (!bound->lazy) {
135 return 0;
136 }
137 tm.tm_isdst = -1;
138 timeval = ts / NSEC_PER_SEC;
139
140 if (bound->lazy_values.gmt) {
141 /* Get day, month, year. */
142 if (!gmtime_r(&timeval, &tm)) {
143 printf_error("Failure in gmtime_r()");
144 goto error;
145 }
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);
150 if (timeval < 0) {
151 printf_error("Failure in timegm(), incorrectly formatted %s timestamp",
152 name);
153 goto error;
154 }
155 } else {
156 /* Get day, month, year. */
157 if (!localtime_r(&timeval, &tm)) {
158 printf_error("Failure in localtime_r()");
159 goto error;
160 }
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);
165 if (timeval < 0) {
166 printf_error("Failure in mktime(), incorrectly formatted %s timestamp",
167 name);
168 goto error;
169 }
170 }
171 value = (int64_t) timeval;
172 value *= NSEC_PER_SEC;
173 value += bound->lazy_values.ns;
174 bound->value = value;
175 bound->set = true;
176 bound->lazy = false;
177 *lazy_update = true;
178 return 0;
179
180 error:
181 return -1;
182 }
183
184 static
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, bool *finished)
190 {
191 int64_t ts;
192 int clock_ret;
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;
203
204 event = bt_notification_event_get_event(notification);
205 assert(event);
206 cc_prio_map = bt_notification_event_get_clock_class_priority_map(
207 notification);
208 assert(cc_prio_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);
213 bt_put(cc_prio_map);
214
215 stream = bt_ctf_event_get_stream(event);
216 assert(stream);
217
218 stream_class = bt_ctf_stream_get_class(stream);
219 assert(stream_class);
220
221 trace = bt_ctf_stream_class_get_trace(stream_class);
222 assert(trace);
223
224 /* FIXME multi-clock? */
225 clock_class = bt_ctf_trace_get_clock_class_by_index(trace, 0);
226 if (!clock_class) {
227 goto end;
228 }
229
230 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
231 if (!clock_value) {
232 printf_error("Failed to retrieve clock value");
233 goto error;
234 }
235
236 clock_ret = bt_ctf_clock_value_get_value_ns_from_epoch(
237 clock_value, &ts);
238 if (clock_ret) {
239 printf_error("Failed to retrieve clock value timestamp");
240 goto error;
241 }
242 if (update_lazy_bound(begin, "begin", ts, &lazy_update)) {
243 goto end;
244 }
245 if (update_lazy_bound(end, "end", ts, &lazy_update)) {
246 goto end;
247 }
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 goto error;
252 }
253 }
254 if (begin->set && ts < begin->value) {
255 in_range = false;
256 }
257 if (end->set && ts > end->value) {
258 in_range = false;
259 *finished = true;
260 }
261
262 goto end;
263
264 error:
265 BT_PUT(new_notification);
266 end:
267 bt_put(event);
268 bt_put(writer_event);
269 bt_put(clock_class);
270 bt_put(trace);
271 bt_put(stream);
272 bt_put(stream_class);
273 bt_put(clock_value);
274 *_event_in_range = in_range;
275 return new_notification;
276 }
277
278 static
279 int ns_from_integer_field(struct bt_ctf_field *integer, int64_t *ns)
280 {
281 int ret = 0;
282 int is_signed;
283 uint64_t raw_clock_value;
284 struct bt_ctf_field_type *integer_type = NULL;
285 struct bt_ctf_clock_class *clock_class = NULL;
286 struct bt_ctf_clock_value *clock_value = NULL;
287
288 integer_type = bt_ctf_field_get_type(integer);
289 assert(integer_type);
290 clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(
291 integer_type);
292 if (!clock_class) {
293 ret = -1;
294 goto end;
295 }
296
297 is_signed = bt_ctf_field_type_integer_get_signed(integer_type);
298 if (!is_signed) {
299 ret = bt_ctf_field_unsigned_integer_get_value(integer,
300 &raw_clock_value);
301 if (ret) {
302 goto end;
303 }
304 } else {
305 /* Signed clock values are unsupported. */
306 goto end;
307 }
308
309 clock_value = bt_ctf_clock_value_create(clock_class, raw_clock_value);
310 if (!clock_value) {
311 goto end;
312 }
313
314 ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns);
315 end:
316 bt_put(integer_type);
317 bt_put(clock_class);
318 bt_put(clock_value);
319 return ret;
320 }
321
322 static uint64_t ns_from_value(uint64_t frequency, uint64_t value)
323 {
324 uint64_t ns;
325
326 if (frequency == NSEC_PER_SEC) {
327 ns = value;
328 } else {
329 ns = (uint64_t) ((1e9 * (double) value) / (double) frequency);
330 }
331
332 return ns;
333 }
334
335 /*
336 * timestamp minus the offset.
337 */
338 static
339 int64_t get_raw_timestamp(struct bt_ctf_packet *writer_packet,
340 int64_t timestamp)
341 {
342 struct bt_ctf_clock_class *writer_clock_class;
343 int64_t sec_offset, cycles_offset, ns;
344 struct bt_ctf_trace *writer_trace;
345 struct bt_ctf_stream *writer_stream;
346 struct bt_ctf_stream_class *writer_stream_class;
347 int ret;
348 uint64_t freq;
349
350 writer_stream = bt_ctf_packet_get_stream(writer_packet);
351 assert(writer_stream);
352
353 writer_stream_class = bt_ctf_stream_get_class(writer_stream);
354 assert(writer_stream_class);
355
356 writer_trace = bt_ctf_stream_class_get_trace(writer_stream_class);
357 assert(writer_trace);
358
359 /* FIXME multi-clock? */
360 writer_clock_class = bt_ctf_trace_get_clock_class_by_index(
361 writer_trace, 0);
362 assert(writer_clock_class);
363
364 ret = bt_ctf_clock_class_get_offset_s(writer_clock_class, &sec_offset);
365 assert(!ret);
366 ns = sec_offset * NSEC_PER_SEC;
367
368 freq = bt_ctf_clock_class_get_frequency(writer_clock_class);
369 assert(freq != -1ULL);
370
371 ret = bt_ctf_clock_class_get_offset_cycles(writer_clock_class, &cycles_offset);
372 assert(!ret);
373
374 ns += ns_from_value(freq, cycles_offset);
375
376 bt_put(writer_clock_class);
377 bt_put(writer_trace);
378 bt_put(writer_stream_class);
379 bt_put(writer_stream);
380
381 return timestamp - ns;
382 }
383
384 static
385 struct bt_notification *evaluate_packet_notification(
386 struct bt_notification *notification,
387 struct trimmer_iterator *trim_it,
388 struct trimmer_bound *begin, struct trimmer_bound *end,
389 bool *_packet_in_range, bool *finished)
390 {
391 int64_t begin_ns, pkt_begin_ns, end_ns, pkt_end_ns;
392 bool in_range = true;
393 struct bt_ctf_packet *packet = NULL, *writer_packet = NULL;
394 struct bt_ctf_field *packet_context = NULL,
395 *timestamp_begin = NULL,
396 *timestamp_end = NULL;
397 struct bt_notification *new_notification = NULL;
398 enum bt_component_status ret;
399 bool lazy_update = false;
400
401 switch (bt_notification_get_type(notification)) {
402 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
403 packet = bt_notification_packet_begin_get_packet(notification);
404 assert(packet);
405 writer_packet = trimmer_new_packet(trim_it, packet);
406 assert(writer_packet);
407 break;
408 case BT_NOTIFICATION_TYPE_PACKET_END:
409 packet = bt_notification_packet_end_get_packet(notification);
410 assert(packet);
411 writer_packet = trimmer_close_packet(trim_it, packet);
412 assert(writer_packet);
413 break;
414 default:
415 goto end;
416 }
417
418 packet_context = bt_ctf_packet_get_context(writer_packet);
419 if (!packet_context) {
420 goto end_no_notif;
421 }
422
423 if (!bt_ctf_field_is_structure(packet_context)) {
424 goto end_no_notif;
425 }
426
427 timestamp_begin = bt_ctf_field_structure_get_field(
428 packet_context, "timestamp_begin");
429 if (!timestamp_begin || !bt_ctf_field_is_integer(timestamp_begin)) {
430 goto end_no_notif;
431 }
432 timestamp_end = bt_ctf_field_structure_get_field(
433 packet_context, "timestamp_end");
434 if (!timestamp_end || !bt_ctf_field_is_integer(timestamp_end)) {
435 goto end_no_notif;
436 }
437
438 if (ns_from_integer_field(timestamp_begin, &pkt_begin_ns)) {
439 goto end_no_notif;
440 }
441 if (ns_from_integer_field(timestamp_end, &pkt_end_ns)) {
442 goto end_no_notif;
443 }
444
445 if (update_lazy_bound(begin, "begin", pkt_begin_ns, &lazy_update)) {
446 goto end_no_notif;
447 }
448 if (update_lazy_bound(end, "end", pkt_end_ns, &lazy_update)) {
449 goto end_no_notif;
450 }
451 if (lazy_update && begin->set && end->set) {
452 if (begin->value > end->value) {
453 printf_error("Unexpected: time range begin value is above end value");
454 goto end_no_notif;
455 }
456 }
457
458 begin_ns = begin->set ? begin->value : INT64_MIN;
459 end_ns = end->set ? end->value : INT64_MAX;
460
461 /*
462 * Accept if there is any overlap between the selected region and the
463 * packet.
464 */
465 in_range = (pkt_end_ns >= begin_ns) && (pkt_begin_ns <= end_ns);
466 if (!in_range) {
467 goto end_no_notif;
468 }
469 if (pkt_begin_ns > end_ns) {
470 *finished = true;
471 }
472
473 if (begin_ns > pkt_begin_ns) {
474 ret = update_packet_context_field(trim_it->err, writer_packet,
475 "timestamp_begin",
476 get_raw_timestamp(writer_packet, begin_ns));
477 assert(!ret);
478 }
479
480 if (end_ns < pkt_end_ns) {
481 ret = update_packet_context_field(trim_it->err, writer_packet,
482 "timestamp_end",
483 get_raw_timestamp(writer_packet, end_ns));
484 assert(!ret);
485 }
486
487 end:
488 switch (bt_notification_get_type(notification)) {
489 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
490 new_notification = bt_notification_packet_begin_create(writer_packet);
491 assert(new_notification);
492 break;
493 case BT_NOTIFICATION_TYPE_PACKET_END:
494 new_notification = bt_notification_packet_end_create(writer_packet);
495 assert(new_notification);
496 break;
497 default:
498 break;
499 }
500 end_no_notif:
501 *_packet_in_range = in_range;
502 bt_put(packet);
503 bt_put(writer_packet);
504 bt_put(packet_context);
505 bt_put(timestamp_begin);
506 bt_put(timestamp_end);
507 return new_notification;
508 }
509
510 static
511 struct bt_notification *evaluate_stream_notification(
512 struct bt_notification *notification,
513 struct trimmer_iterator *trim_it)
514 {
515 struct bt_ctf_stream *stream;
516
517 stream = bt_notification_stream_end_get_stream(notification);
518 assert(stream);
519
520 /* FIXME: useless copy */
521 return bt_notification_stream_end_create(stream);
522 }
523
524 /* Return true if the notification should be forwarded. */
525 static
526 enum bt_notification_iterator_status evaluate_notification(
527 struct bt_notification **notification,
528 struct trimmer_iterator *trim_it,
529 struct trimmer_bound *begin, struct trimmer_bound *end,
530 bool *in_range)
531 {
532 enum bt_notification_type type;
533 struct bt_notification *new_notification = NULL;
534 bool finished = false;
535
536 *in_range = true;
537 type = bt_notification_get_type(*notification);
538 switch (type) {
539 case BT_NOTIFICATION_TYPE_EVENT:
540 new_notification = evaluate_event_notification(*notification,
541 trim_it, begin, end, in_range, &finished);
542 break;
543 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
544 case BT_NOTIFICATION_TYPE_PACKET_END:
545 new_notification = evaluate_packet_notification(*notification,
546 trim_it, begin, end, in_range, &finished);
547 break;
548 case BT_NOTIFICATION_TYPE_STREAM_END:
549 new_notification = evaluate_stream_notification(*notification,
550 trim_it);
551 break;
552 default:
553 puts("Unhandled notification type");
554 break;
555 }
556 BT_PUT(*notification);
557 *notification = new_notification;
558
559 if (finished) {
560 return BT_NOTIFICATION_ITERATOR_STATUS_END;
561 }
562
563 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
564 }
565
566 BT_HIDDEN
567 struct bt_notification_iterator_next_return trimmer_iterator_next(
568 struct bt_private_notification_iterator *iterator)
569 {
570 struct trimmer_iterator *trim_it = NULL;
571 struct bt_private_component *component = NULL;
572 struct trimmer *trimmer = NULL;
573 struct bt_notification_iterator *source_it = NULL;
574 struct bt_notification_iterator_next_return ret = {
575 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
576 .notification = NULL,
577 };
578 bool notification_in_range = false;
579
580 trim_it = bt_private_notification_iterator_get_user_data(iterator);
581 assert(trim_it);
582
583 component = bt_private_notification_iterator_get_private_component(
584 iterator);
585 assert(component);
586 trimmer = bt_private_component_get_user_data(component);
587 assert(trimmer);
588
589 source_it = trim_it->input_iterator;
590 assert(source_it);
591
592 while (!notification_in_range) {
593 ret.status = bt_notification_iterator_next(source_it);
594 if (ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
595 goto end;
596 }
597
598 ret.notification = bt_notification_iterator_get_notification(
599 source_it);
600 if (!ret.notification) {
601 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
602 goto end;
603 }
604
605 ret.status = evaluate_notification(&ret.notification, trim_it,
606 &trimmer->begin, &trimmer->end,
607 &notification_in_range);
608 if (!notification_in_range) {
609 BT_PUT(ret.notification);
610 }
611
612 if (ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
613 break;
614 }
615 }
616 end:
617 bt_put(component);
618 return ret;
619 }
620
621 BT_HIDDEN
622 enum bt_notification_iterator_status trimmer_iterator_seek_time(
623 struct bt_private_notification_iterator *iterator,
624 int64_t time)
625 {
626 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
627 }
This page took 0.042468 seconds and 4 git commands to generate.