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