Remove the need to implement the notification iterator's "get" method
[babeltrace.git] / plugins / utils / trimmer / iterator.c
CommitLineData
cab3f160
JG
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 "trimmer.h"
30#include "iterator.h"
33b34c43 31#include <babeltrace/component/notification/iterator.h>
890882ef 32#include <babeltrace/component/notification/private-iterator.h>
33b34c43
PP
33#include <babeltrace/component/notification/notification.h>
34#include <babeltrace/component/notification/event.h>
35#include <babeltrace/component/notification/stream.h>
36#include <babeltrace/component/notification/packet.h>
d71dcf2c 37#include <babeltrace/component/component-filter.h>
890882ef
PP
38#include <babeltrace/component/private-component-filter.h>
39#include <babeltrace/component/private-port.h>
40#include <babeltrace/component/private-connection.h>
41#include <babeltrace/component/private-component.h>
44d3cbf0
JG
42#include <babeltrace/ctf-ir/event.h>
43#include <babeltrace/ctf-ir/stream.h>
44#include <babeltrace/ctf-ir/stream-class.h>
ac0c6bdd 45#include <babeltrace/ctf-ir/clock-class.h>
44d3cbf0
JG
46#include <babeltrace/ctf-ir/packet.h>
47#include <babeltrace/ctf-ir/trace.h>
72208f03 48#include <babeltrace/ctf-ir/fields.h>
44d3cbf0 49#include <assert.h>
8b0ce102 50#include <plugins-common.h>
44d3cbf0 51
d3eb6e8f 52BT_HIDDEN
64cadc66 53void trimmer_iterator_finalize(struct bt_private_notification_iterator *it)
44d3cbf0
JG
54{
55 struct trimmer_iterator *it_data;
56
890882ef 57 it_data = bt_private_notification_iterator_get_user_data(it);
44d3cbf0
JG
58 assert(it_data);
59
99cdb69e 60 bt_put(it_data->input_iterator);
44d3cbf0
JG
61 g_free(it_data);
62}
cab3f160
JG
63
64BT_HIDDEN
d3eb6e8f 65enum bt_notification_iterator_status trimmer_iterator_init(
890882ef
PP
66 struct bt_private_component *component,
67 struct bt_private_port *port,
68 struct bt_private_notification_iterator *iterator)
cab3f160 69{
d3eb6e8f
PP
70 enum bt_notification_iterator_status ret =
71 BT_NOTIFICATION_ITERATOR_STATUS_OK;
cab3f160 72 enum bt_notification_iterator_status it_ret;
890882ef
PP
73 struct bt_private_port *input_port = NULL;
74 struct bt_private_connection *connection = NULL;
44d3cbf0
JG
75 struct trimmer_iterator *it_data = g_new0(struct trimmer_iterator, 1);
76
77 if (!it_data) {
d3eb6e8f 78 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
44d3cbf0
JG
79 goto end;
80 }
81
99cdb69e 82 /* Create a new iterator on the upstream component. */
890882ef
PP
83 input_port = bt_private_component_filter_get_default_input_private_port(
84 component);
99cdb69e 85 assert(input_port);
890882ef 86 connection = bt_private_port_get_private_connection(input_port);
99cdb69e
JG
87 assert(connection);
88
890882ef
PP
89 it_data->input_iterator =
90 bt_private_connection_create_notification_iterator(connection);
99cdb69e
JG
91 if (!it_data->input_iterator) {
92 ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
93 goto end;
94 }
95
890882ef
PP
96 it_ret = bt_private_notification_iterator_set_user_data(iterator,
97 it_data);
44d3cbf0
JG
98 if (it_ret) {
99 goto end;
100 }
cab3f160 101end:
99cdb69e
JG
102 bt_put(connection);
103 bt_put(input_port);
cab3f160
JG
104 return ret;
105}
106
528debdf
MD
107static
108int update_lazy_bound(struct trimmer_bound *bound, const char *name,
55595636 109 int64_t ts, bool *lazy_update)
528debdf
MD
110{
111 struct tm tm;
112 int64_t value;
113 time_t timeval;
114
55595636
MD
115 *lazy_update = false;
116
528debdf
MD
117 if (!bound->lazy) {
118 return 0;
119 }
120 tm.tm_isdst = -1;
121 timeval = ts / NSEC_PER_SEC;
122
123 if (bound->lazy_values.gmt) {
124 /* Get day, month, year. */
125 if (!gmtime_r(&timeval, &tm)) {
55595636 126 printf_error("Failure in gmtime_r()");
528debdf
MD
127 goto error;
128 }
129 tm.tm_sec = bound->lazy_values.ss;
130 tm.tm_min = bound->lazy_values.mm;
131 tm.tm_hour = bound->lazy_values.hh;
132 timeval = timegm(&tm);
133 if (timeval < 0) {
55595636
MD
134 printf_error("Failure in timegm(), incorrectly formatted %s timestamp",
135 name);
528debdf
MD
136 goto error;
137 }
138 } else {
139 /* Get day, month, year. */
140 if (!localtime_r(&timeval, &tm)) {
55595636 141 printf_error("Failure in localtime_r()");
528debdf
MD
142 goto error;
143 }
144 tm.tm_sec = bound->lazy_values.ss;
145 tm.tm_min = bound->lazy_values.mm;
146 tm.tm_hour = bound->lazy_values.hh;
147 timeval = mktime(&tm);
148 if (timeval < 0) {
55595636 149 printf_error("Failure in mktime(), incorrectly formatted %s timestamp",
528debdf
MD
150 name);
151 goto error;
152 }
153 }
154 value = (int64_t) timeval;
155 value *= NSEC_PER_SEC;
156 value += bound->lazy_values.ns;
157 bound->value = value;
158 bound->set = true;
159 bound->lazy = false;
55595636 160 *lazy_update = true;
528debdf
MD
161 return 0;
162
163error:
528debdf
MD
164 return -1;
165}
166
44d3cbf0 167static
55595636
MD
168enum bt_notification_iterator_status
169evaluate_event_notification(struct bt_notification *notification,
170 struct trimmer_bound *begin, struct trimmer_bound *end,
171 bool *_event_in_range)
44d3cbf0 172{
72208f03
JG
173 int64_t ts;
174 int clock_ret;
175 struct bt_ctf_event *event = NULL;
176 bool in_range = true;
ac0c6bdd 177 struct bt_ctf_clock_class *clock_class = NULL;
72208f03 178 struct bt_ctf_trace *trace = NULL;
44d3cbf0 179 struct bt_ctf_stream *stream = NULL;
72208f03
JG
180 struct bt_ctf_stream_class *stream_class = NULL;
181 struct bt_ctf_clock_value *clock_value = NULL;
55595636
MD
182 enum bt_notification_iterator_status ret =
183 BT_NOTIFICATION_ITERATOR_STATUS_OK;
184 bool lazy_update = false;
44d3cbf0 185
72208f03
JG
186 event = bt_notification_event_get_event(notification);
187 assert(event);
44d3cbf0 188
72208f03
JG
189 stream = bt_ctf_event_get_stream(event);
190 assert(stream);
44d3cbf0 191
72208f03
JG
192 stream_class = bt_ctf_stream_get_class(stream);
193 assert(stream_class);
194
195 trace = bt_ctf_stream_class_get_trace(stream_class);
196 assert(trace);
197
198 /* FIXME multi-clock? */
ac0c6bdd
PP
199 clock_class = bt_ctf_trace_get_clock_class(trace, 0);
200 if (!clock_class) {
72208f03 201 goto end;
44d3cbf0 202 }
44d3cbf0 203
ac0c6bdd 204 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
72208f03 205 if (!clock_value) {
55595636
MD
206 printf_error("Failed to retrieve clock value");
207 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
72208f03 208 goto end;
44d3cbf0 209 }
72208f03
JG
210
211 clock_ret = bt_ctf_clock_value_get_value_ns_from_epoch(
212 clock_value, &ts);
213 if (clock_ret) {
55595636
MD
214 printf_error("Failed to retrieve clock value timestamp");
215 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
44d3cbf0
JG
216 goto end;
217 }
55595636
MD
218 if (update_lazy_bound(begin, "begin", ts, &lazy_update)) {
219 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
528debdf
MD
220 goto end;
221 }
55595636
MD
222 if (update_lazy_bound(end, "end", ts, &lazy_update)) {
223 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
528debdf
MD
224 goto end;
225 }
55595636
MD
226 if (lazy_update && begin->set && end->set) {
227 if (begin->value > end->value) {
228 printf_error("Unexpected: time range begin value is above end value");
229 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
230 goto end;
231 }
232 }
72208f03
JG
233 if (begin->set && ts < begin->value) {
234 in_range = false;
235 }
236 if (end->set && ts > end->value) {
237 in_range = false;
238 }
44d3cbf0 239end:
72208f03 240 bt_put(event);
ac0c6bdd 241 bt_put(clock_class);
72208f03
JG
242 bt_put(trace);
243 bt_put(stream);
244 bt_put(stream_class);
245 bt_put(clock_value);
55595636
MD
246 *_event_in_range = in_range;
247 return ret;
44d3cbf0
JG
248}
249
44d3cbf0 250static
72208f03 251int ns_from_integer_field(struct bt_ctf_field *integer, int64_t *ns)
44d3cbf0 252{
72208f03
JG
253 int ret = 0;
254 int is_signed;
255 uint64_t raw_clock_value;
256 struct bt_ctf_field_type *integer_type = NULL;
ac0c6bdd 257 struct bt_ctf_clock_class *clock_class = NULL;
44d3cbf0
JG
258 struct bt_ctf_clock_value *clock_value = NULL;
259
72208f03
JG
260 integer_type = bt_ctf_field_get_type(integer);
261 assert(integer_type);
ac0c6bdd
PP
262 clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(
263 integer_type);
264 if (!clock_class) {
72208f03 265 ret = -1;
44d3cbf0
JG
266 goto end;
267 }
268
72208f03
JG
269 is_signed = bt_ctf_field_type_integer_get_signed(integer_type);
270 if (!is_signed) {
271 ret = bt_ctf_field_unsigned_integer_get_value(integer,
272 &raw_clock_value);
273 if (ret) {
44d3cbf0
JG
274 goto end;
275 }
72208f03
JG
276 } else {
277 /* Signed clock values are unsupported. */
278 goto end;
279 }
44d3cbf0 280
ac0c6bdd 281 clock_value = bt_ctf_clock_value_create(clock_class, raw_clock_value);
72208f03
JG
282 if (!clock_value) {
283 goto end;
284 }
44d3cbf0 285
72208f03
JG
286 ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns);
287end:
288 bt_put(integer_type);
ac0c6bdd 289 bt_put(clock_class);
72208f03
JG
290 bt_put(clock_value);
291 return ret;
292}
44d3cbf0 293
72208f03 294static
55595636
MD
295enum bt_notification_iterator_status evaluate_packet_notification(
296 struct bt_notification *notification,
297 struct trimmer_bound *begin, struct trimmer_bound *end,
298 bool *_packet_in_range)
72208f03 299{
55595636
MD
300 enum bt_notification_iterator_status ret =
301 BT_NOTIFICATION_ITERATOR_STATUS_OK;
72208f03
JG
302 int64_t begin_ns, pkt_begin_ns, end_ns, pkt_end_ns;
303 bool in_range = true;
304 struct bt_ctf_packet *packet = NULL;
305 struct bt_ctf_field *packet_context = NULL,
306 *timestamp_begin = NULL,
307 *timestamp_end = NULL;
308
309 switch (bt_notification_get_type(notification)) {
310 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
311 packet = bt_notification_packet_begin_get_packet(notification);
44d3cbf0 312 break;
72208f03
JG
313 case BT_NOTIFICATION_TYPE_PACKET_END:
314 packet = bt_notification_packet_end_get_packet(notification);
315 break;
316 default:
55595636 317 break;
44d3cbf0 318 }
72208f03
JG
319 assert(packet);
320
321 packet_context = bt_ctf_packet_get_context(packet);
322 if (!packet_context) {
323 goto end;
324 }
325
326 if (!bt_ctf_field_is_structure(packet_context)) {
327 goto end;
328 }
329
330 timestamp_begin = bt_ctf_field_structure_get_field(
331 packet_context, "timestamp_begin");
332 if (!timestamp_begin || !bt_ctf_field_is_integer(timestamp_begin)) {
333 goto end;
334 }
335 timestamp_end = bt_ctf_field_structure_get_field(
336 packet_context, "timestamp_end");
337 if (!timestamp_end || !bt_ctf_field_is_integer(timestamp_end)) {
338 goto end;
339 }
340
55595636 341 if (ns_from_integer_field(timestamp_begin, &pkt_begin_ns)) {
72208f03
JG
342 goto end;
343 }
55595636 344 if (ns_from_integer_field(timestamp_end, &pkt_end_ns)) {
72208f03
JG
345 goto end;
346 }
347
348 begin_ns = begin->set ? begin->value : INT64_MIN;
349 end_ns = end->set ? end->value : INT64_MAX;
350
351 /*
352 * Accept if there is any overlap between the selected region and the
353 * packet.
354 */
355 in_range = (pkt_end_ns >= begin_ns) && (pkt_begin_ns <= end_ns);
356end:
55595636 357 *_packet_in_range = in_range;
72208f03
JG
358 bt_put(packet);
359 bt_put(packet_context);
360 bt_put(timestamp_begin);
361 bt_put(timestamp_end);
55595636 362 return ret;
72208f03
JG
363}
364
365/* Return true if the notification should be forwarded. */
366static
55595636
MD
367enum bt_notification_iterator_status evaluate_notification(
368 struct bt_notification *notification,
369 struct trimmer_bound *begin, struct trimmer_bound *end,
370 bool *in_range)
72208f03 371{
72208f03 372 enum bt_notification_type type;
55595636
MD
373 enum bt_notification_iterator_status ret =
374 BT_NOTIFICATION_ITERATOR_STATUS_OK;
72208f03 375
b61397c4 376 *in_range = true;
72208f03
JG
377 type = bt_notification_get_type(notification);
378 switch (type) {
379 case BT_NOTIFICATION_TYPE_EVENT:
55595636
MD
380 ret = evaluate_event_notification(notification, begin,
381 end, in_range);
72208f03
JG
382 break;
383 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
384 case BT_NOTIFICATION_TYPE_PACKET_END:
55595636
MD
385 ret = evaluate_packet_notification(notification, begin,
386 end, in_range);
72208f03 387 break;
44d3cbf0 388 default:
72208f03 389 /* Accept all other notifications. */
44d3cbf0
JG
390 break;
391 }
55595636 392 return ret;
cab3f160
JG
393}
394
395BT_HIDDEN
41a2b7ae 396struct bt_notification_iterator_next_return trimmer_iterator_next(
890882ef 397 struct bt_private_notification_iterator *iterator)
cab3f160 398{
44d3cbf0 399 struct trimmer_iterator *trim_it = NULL;
890882ef 400 struct bt_private_component *component = NULL;
44d3cbf0
JG
401 struct trimmer *trimmer = NULL;
402 struct bt_notification_iterator *source_it = NULL;
41a2b7ae
PP
403 struct bt_notification_iterator_next_return ret = {
404 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
405 .notification = NULL,
406 };
72208f03 407 bool notification_in_range = false;
cab3f160 408
890882ef 409 trim_it = bt_private_notification_iterator_get_user_data(iterator);
44d3cbf0
JG
410 assert(trim_it);
411
890882ef
PP
412 component = bt_private_notification_iterator_get_private_component(
413 iterator);
44d3cbf0 414 assert(component);
890882ef 415 trimmer = bt_private_component_get_user_data(component);
44d3cbf0
JG
416 assert(trimmer);
417
99cdb69e
JG
418 source_it = trim_it->input_iterator;
419 assert(source_it);
44d3cbf0 420
72208f03 421 while (!notification_in_range) {
41a2b7ae
PP
422 ret.status = bt_notification_iterator_next(source_it);
423 if (ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
44d3cbf0
JG
424 goto end;
425 }
426
41a2b7ae 427 ret.notification = bt_notification_iterator_get_notification(
44d3cbf0 428 source_it);
41a2b7ae
PP
429 if (!ret.notification) {
430 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
44d3cbf0
JG
431 goto end;
432 }
433
41a2b7ae 434 ret.status = evaluate_notification(ret.notification,
55595636
MD
435 &trimmer->begin, &trimmer->end,
436 &notification_in_range);
41a2b7ae
PP
437 if (!notification_in_range) {
438 bt_put(ret.notification);
44d3cbf0 439 }
6d5f6792 440
41a2b7ae 441 if (ret.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
6d5f6792
JG
442 break;
443 }
44d3cbf0 444 }
cab3f160 445end:
44d3cbf0 446 bt_put(component);
cab3f160
JG
447 return ret;
448}
449
450BT_HIDDEN
451enum bt_notification_iterator_status trimmer_iterator_seek_time(
890882ef
PP
452 struct bt_private_notification_iterator *iterator,
453 int64_t time)
cab3f160 454{
72b913fb 455 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
cab3f160 456}
This page took 0.047148 seconds and 4 git commands to generate.