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