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