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